config

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: GPL-3.0 Imports: 6 Imported by: 4

README

Config

支持加载ini格式的配置文件
支持以;或者#开头的注释

example

type testConf struct {
	DB struct {
		Domain string
		Port   int `default:"9088"`
		Enable bool
	}
    aaa int
}

var conf testConf
if err := LoadConfig(path, &conf); err != nil {
    t.Fatalf(errors.ErrorStack(err))
}
t.Logf("conf:%+v", conf)

配置文件

 [db]
domain    =jd.com
enable=true
# test comments
;port=3306

只要传入对应的ini文件全路径及struct指针就可以了,简单高效.
运行结果:

conf:{DB:{Domain:jd.com Port:9088 Enable:true} aaa:0}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrUnsupported 不支持类型
	ErrUnsupported = fmt.Errorf("expect ptr data")
	//ErrInvalidType 参数必须为指针类型
	ErrInvalidType = fmt.Errorf("result must be ptr")
	// ErrNotFound 没找到key
	ErrNotFound = fmt.Errorf("key not found")
)

Functions

func LoadConfig

func LoadConfig(path string, result interface{}) error

LoadConfig 加载文件形式配置文件, 并解析成指定结构.

func ParseConfig added in v1.6.1

func ParseConfig(body string, result interface{}) error

ParseConfig 解析内存中配置文件.

Types

type Config

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

Config 读ini配置文件.

func NewConfig

func NewConfig(path, body string) (c *Config, err error)

NewConfig 加载配置文件.

func (*Config) GetData

func (c *Config) GetData(s, k string, result interface{}, d interface{}) error

GetData 获取指定段的指定key的值, 支持int,string.

func (*Config) Parse added in v1.6.1

func (c *Config) Parse(result interface{}) error

Parse 根据result结构读配置文件.

Jump to

Keyboard shortcuts

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