3 Star 0 Fork 0

neuro-netw0rk/server-golib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
conf.go 859 Bytes
一键复制 编辑 原始数据 按行查看 历史
LeoWang 提交于 7个月前 . 整合golib和serverlib
package utils
import (
"encoding/json"
"os"
"strings"
"github.com/spf13/viper"
)
type Conf struct {
*viper.Viper
Name string
Data interface{}
}
func (conf *Conf) Sub(key string) *Conf {
return &Conf{Viper: conf.Viper.Sub(key), Name: conf.Name}
}
func Load(name string, data interface{}) (*Conf, error) {
if data == nil {
data = &map[string]interface{}{}
}
conf := &Conf{Viper: viper.New(), Name: name, Data: data}
if name == "" {
return conf, nil
}
conf.Viper.SetConfigFile(name)
if e := conf.Viper.ReadInConfig(); e != nil {
if strings.HasSuffix(name, ".json") {
// 兼容JSON数组
if f, e := os.Open(name); e != nil {
return conf, e
} else if e := json.NewDecoder(f).Decode(&conf.Data); e != nil {
return conf, e
}
return conf, nil
}
return conf, e
}
return conf, conf.Viper.Unmarshal(conf.Data)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/neuro-netw0rk/server-golib.git
git@gitee.com:neuro-netw0rk/server-golib.git
neuro-netw0rk
server-golib
server-golib
v0.0.5-beta2

搜索帮助