type ServerConfig struct {
	URL string
	Timeout time.Duration
    Size int
    A  int
    B int
    C int
}
ServerConfig 是从配置文件反序列化出来的,里面的变量如果配置文件没有提供的话,很多变量都是零值, 但是我期望里面的很多变量都是我自己定义的一个默认值。
我现在的做法是反序列化后再一个一个的判断,如果是零值,就改成我期望的值,这样感觉比较麻烦,有什么其他更好的方法吗?
|      1blackcurrant      2023-11-22 03:33:20 +08:00 使用 viper 读取配置。 然后类似这样定义 type RPCConfig struct { Host string `mapstructure:"host"` Port int `mapstructure:"port"` // 使用默认值 Timeout int `mapstructure:"timeout,default=30"` } | 
|  |      2mornlight      2023-11-22 05:31:40 +08:00 做配置管理的第三方库很多都支持 default tag 。 如果觉得不好用可以先用这个库 Set 一遍 github.com/creasty/defaults | 
|      38X96ZltB8D7WggD7      2023-11-22 08:38:04 +08:00 先赋默认值,再反序列化 | 
|  |      4CyJaySong      2023-11-22 08:38:22 +08:00  2 | 
|      5yuancoder      2023-11-22 09:16:40 +08:00 一般是通过 tag 实现的 | 
|  |      6ikaros      2023-11-22 09:17:55 +08:00 我的看法是为了这么简单的需求引入一个新的库不值(你甚至只用了他的很少功能),如果是基于 tag 的反射还影响性能, 个人解决方法偏向于给这个 struct 写个 SetDefaultValue 的方法,unmarshal 完之后调用一下 | 
|      7ding2dong      2023-11-22 09:24:13 +08:00  1 func GetDefaultServerConfig() ServerConfig { return ServerConfig{ // 你的一些默认值... } } //读取配置 serverConfig := GetDefaultServerConfig() json.Unmarshal([]byte(jsonStr), &serverConfig) | 
|  |      9dw2693734d      2023-11-22 09:28:34 +08:00 @CyJaySong 这种比较简单,我也这样用 | 
|      10fo0o7hU2tr6v6TCe      2023-11-22 09:30:05 +08:00 https://blog.51cto.com/hongchen99/4520434 tag 实现,重写方法? | 
|  |      11CzaOrz      2023-11-22 09:45:09 +08:00 看着有点眼熟,可以参考我自己写的一个工具库,原理就是基于反射解析 Tag 然后赋值即可: - https://github.com/czasg/go-fill ```go // 依赖 import "github.com/czasg/go-fill" // 准备结构体 type Config struct { Host string `fill:"HOST,default=localhost"` Port int `fill:"PORT,default=5432"` User string `fill:"USER,default=root"` Password string `fill:"PASSWORD,default=root"` } // 初始化 cfg := Config{} // 填充环境变量 _ = fill.FillEnv(&cfg) ``` | 
|      12yujianwjj OP ``` type Config struct { ServerConfigs []ServerConfig } ``` 如果 先赋默认值,再反序列化的话。针对这个情况咋搞? | 
|  |      13fantastM      2023-11-22 11:37:13 +08:00 @yujianwjj #12 如果解析的是 yaml 配置的话,可以试试这种方式 https://github.com/go-yaml/yaml/issues/165#issuecomment-255223956 |