## 功能介绍
1 设置默认值
2 从json toml hcl 读取配置
3 从环境变量env 读取值
4 读缓冲区
5 远程读取配置文件
6 key不区分大小写
## cobra配置viper
cobra使用viper来加载配置
```
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".demo" (without extension).
viper.AddConfigPath(home)
// 当前目录下寻找配置文件
viper.AddConfigPath(".")
viper.SetConfigName(".demo")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
// Unmarshal 将配置文件转成对象
cfgErr = viper.Unmarshal(&cfg)
}
}
```
使用配置启动程序:
```
var testCmd = &cobra.Command{
Use: "test",
Short: "A brief description of your command",
Long: `what`,
Run: serve,
}
func init() {
RootCmd.AddCommand(testCmd)
}
func serve(cmd *cobra.Command, args []string) {
if cfgErr != nil {
fmt.Println(fmt.Errorf("Config error: %s", cfgErr))
return
}
demo.Run(&cfg)
}
```
- 命令行库cobra
- 用户路径检测go-homedir
- 配置解决方案viper(cobra配置用)
- 高效结构化日志库zap
- RPC框架grpc
- mongdb操作mgo
- ORM库xorm
- GRPCrest接口grpcgateway
- 使用gogoproto时grpcgateway的protobuf和json转换方法
- sync.Map
- zmq
- gogoproto
- go类型转换和类型断言
- go select用法详解以及定时器
- go并发资源竞争
- 官方命令行库flag
- 配置文件解析器 robig/config
- interface {} 接口
- goroutine && channel
- go 命名
- 类型switch
- 数据
- 初始化
- 指针方法 && 值方法
- 内嵌
- mqtt go实现
- grpc middleware