9 Star 0 Fork 7

sparrow/sparrow_cloud_go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
setting.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
飞向未来 提交于 2024-04-07 11:34 . init
package setting
import (
"fmt"
"strings"
"github.com/spf13/viper"
)
// import (
// "fmt"
// "strings"
// "github.com/spf13/viper"
// )
type Settings = viper.Viper
var s *Settings
// 导出函数, 如果需要使用其他函数. 参考: https://github.com/spf13/viper/blob/master/viper.go
var (
GetString = viper.GetString // 获取字符串
GetBool = viper.GetBool // 获取 bool
GetDuration = viper.GetDuration // 获取时间长度
GetInt = viper.GetInt // 获取整形
GetInt32 = viper.GetInt32
GetInt64 = viper.GetInt64 // 获取64位整形
GetUint = viper.GetUint
GetUint16 = viper.GetUint16
GetUint32 = viper.GetUint32
GetUint64 = viper.GetUint64
GetTime = viper.GetTime
GetIntSlice = viper.GetIntSlice
GetFloat64 = viper.GetFloat64
GetStringSlice = viper.GetStringSlice
GetStringMap = viper.GetStringMap
GetStringMapString = viper.GetStringMapString
GetStringMapStringSlice = viper.GetStringMapStringSlice
GetSizeInBytes = viper.GetSizeInBytes
)
func NewSettings(fileName string) *Settings {
if fileName != "" {
viper.SetConfigName(fileName) // name of config file (without extension)
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
// panic(fmt.Errorf("配置文件不存在: %s", err))
fmt.Printf("配置文件不存在: %s\n", err)
// myLogger.Error("当前文件夹没有找到 配置文件")
}
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
return viper.GetViper()
}
func Init(fileName string) *Settings {
s = NewSettings(fileName)
return s
}
func GetSettings() *Settings {
if s == nil {
panic("请先初始化settings")
}
return s
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/sparrow614/sparrow_cloud_go.git
git@gitee.com:sparrow614/sparrow_cloud_go.git
sparrow614
sparrow_cloud_go
sparrow_cloud_go
v0.13.0

搜索帮助