config

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2021 License: GPL-3.0 Imports: 10 Imported by: 6

README

config

build codecov GitHub

配置文件解析,目前支持toml、yaml、json文件格式。支持多配置文件重载。默认初次加载所有环境变量。支持配置文件热更新。

example:

    import github.com/yuanzhangcai/config

    // 加载配置
    config.LoadFile(configFilePath + "config.toml")

    // 读取配置参数
    value := config.GetString("key")

    // 修改配置参数
    config.Set("newKey", "value")

    value := strut {
      List   []string `json:"list"`
      Server string   `json:"server"`
      Count  int      `json:"count"`
    }{}
    err = Scan([]string{"db"}, &value)

性能压测:

goos: darwin
goarch: amd64
pkg: github.com/yuanzhangcai/config
BenchmarkGet/Get_______________-12         	 6036589	       196 ns/op
BenchmarkGet/viper_Get_________-12       	  462037	      2604 ns/op
BenchmarkGet/micro_config_Get__-12      	 4340478	       275 ns/op
BenchmarkGet/beego_config_Get__-12      	 2691524	       446 ns/op
BenchmarkGet/Parallel_Get______-12      	18792312	        68.7 ns/op
BenchmarkGet/Parallel_viper_Get-12       	 1757396	       668 ns/op
BenchmarkGet/Parallel_micro_Get-12       	13200244	        87.0 ns/op
BenchmarkGet/Parallel_beego_Get-12       	 9161379	       149 ns/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedFileFormat 暂不支持该配置文件格式。
	ErrUnsupportedFileFormat = errors.New("暂不支持该配置文件格式。")
)

Functions

func Get

func Get(keys ...string) interface{}

Get 获取interface配置

func GetArray

func GetArray(keys ...string) []interface{}

GetArray 获取interface数组

func GetBool

func GetBool(keys ...string) bool

GetBool 获取bool配置

func GetFloat64

func GetFloat64(keys ...string) float64

GetFloat64 获取float64配置

func GetInt

func GetInt(keys ...string) int

GetInt 获取int配置

func GetInt64

func GetInt64(keys ...string) int64

GetInt64 获取int64配置

func GetMap

func GetMap(keys ...string) map[string]interface{}

GetMap 获取map数据

func GetString

func GetString(keys ...string) string

GetString 获取配置

func GetStringArray

func GetStringArray(keys ...string) []string

GetStringArray 获取map数据

func GetStringMap

func GetStringMap(keys ...string) map[string]string

GetStringMap 获取string map数据

func GetUint64

func GetUint64(keys ...string) uint64

GetUint64 获取uint64配置

func LoadFile

func LoadFile(file string) error

LoadFile 加载配置文件

func LoadMemory

func LoadMemory(config, t string) error

LoadMemory 从内存中加载配置文件

func LoadOsEnv

func LoadOsEnv() error

LoadOsEnv 加载环境变量

func Scan

func Scan(keys []string, value interface{}) error

Scan 读取配置到指定对象

func Set

func Set(key string, value interface{})

Set 写入配置

func SetPath

func SetPath(keys []string, value interface{})

SetPath 写入多级配置

Types

type Config

type Config interface {
	Loader
	Getter
	Setter
}

Config 配置结构体

func New

func New() Config

New 创建配置文件

type Getter

type Getter interface {
	Get(keys ...string) interface{}
	GetString(keys ...string) string
	GetStringArray(keys ...string) []string
	GetBool(keys ...string) bool
	GetInt(keys ...string) int
	GetInt64(keys ...string) int64
	GetUint64(keys ...string) uint64
	GetFloat64(keys ...string) float64
	GetArray(keys ...string) []interface{}
	GetMap(keys ...string) map[string]interface{}
	GetStringMap(keys ...string) map[string]string
	Scan(keys []string, value interface{}) error
}

Getter 获取接口

type JSONConfig

type JSONConfig struct {
	// contains filtered or unexported fields
}

JSONConfig json配置文件

func (*JSONConfig) Get

func (c *JSONConfig) Get(keys ...string) interface{}

Get 获取interface配置

func (*JSONConfig) GetArray

func (c *JSONConfig) GetArray(keys ...string) []interface{}

GetArray 获取interface数组

func (*JSONConfig) GetBool

func (c *JSONConfig) GetBool(keys ...string) bool

GetBool 获取bool配置

func (*JSONConfig) GetFloat64

func (c *JSONConfig) GetFloat64(keys ...string) float64

GetFloat64 获取float64配置

func (*JSONConfig) GetInt

func (c *JSONConfig) GetInt(keys ...string) int

GetInt 获取int配置

func (*JSONConfig) GetInt64

func (c *JSONConfig) GetInt64(keys ...string) int64

GetInt64 获取int64配置

func (*JSONConfig) GetMap

func (c *JSONConfig) GetMap(keys ...string) map[string]interface{}

GetMap 获取map数据

func (*JSONConfig) GetString

func (c *JSONConfig) GetString(keys ...string) string

GetString 获取string配置

func (*JSONConfig) GetStringArray

func (c *JSONConfig) GetStringArray(keys ...string) []string

GetStringArray 获取map数据

func (*JSONConfig) GetStringMap

func (c *JSONConfig) GetStringMap(keys ...string) map[string]string

GetStringMap 获取map数据

func (*JSONConfig) GetUint64

func (c *JSONConfig) GetUint64(keys ...string) uint64

GetUint64 获取uint64配置

func (*JSONConfig) LoadFile

func (c *JSONConfig) LoadFile(file string) error

LoadFile 加载配置文件

func (*JSONConfig) LoadMemory

func (c *JSONConfig) LoadMemory(config, t string) error

LoadMemory 加载内存配置

func (*JSONConfig) LoadOsEnv

func (c *JSONConfig) LoadOsEnv() error

LoadOsEnv 载入环境变量

func (*JSONConfig) Scan

func (c *JSONConfig) Scan(keys []string, value interface{}) error

Scan 读取配置到指定对象

func (*JSONConfig) Set

func (c *JSONConfig) Set(key string, value interface{})

Set 写入配置

func (*JSONConfig) SetPath

func (c *JSONConfig) SetPath(keys []string, value interface{})

SetPath 写入多级配置

type Loader

type Loader interface {
	LoadMemory(config, t string) error // 加载内存配置
	LoadFile(file string) error        // 加载配置文件
	LoadOsEnv() error                  // 加载环境变量
}

Loader 加载器接口

type Setter

type Setter interface {
	Set(key string, value interface{})
	SetPath(keys []string, value interface{})
}

Setter 设置接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL