Go 语言学习手册

免费学习 完整知识体系 快速检索

Go语言学习手册《配置管理(Viper)》:Viper 是 Go 流行的配置管理库,支持多种格式(JSON, YAML, TOML, env)。 **安装:** `go get github.com/spf13/viper` **基本使用:** ```go import "git...

配置与日志

配置管理(Viper)

【详细说明 & 代码示例】
Viper 是 Go 流行的配置管理库,支持多种格式(JSON, YAML, TOML, env)。 **安装:** `go get github.com/spf13/viper` **基本使用:** ```go import "github.com/spf13/viper" // 设置配置文件名和路径 viper.SetConfigName("config") // 不含扩展名 viper.SetConfigType("yaml") viper.AddConfigPath("./configs") viper.AddConfigPath(".") // 读取配置 if err := viper.ReadInConfig(); err != nil { panic(err) } // 获取值 port := viper.GetInt("server.port") name := viper.GetString("app.name") // 绑定环境变量 viper.AutomaticEnv() ``` **配置文件示例(config.yaml):** ```yaml server: port: 8080 timeout: 30 app: name: myapp ``` **默认值:** `viper.SetDefault("server.port", 8080)` **实时监听:** `viper.WatchConfig()` 可在配置变化时触发回调。

学习提示:建议安装 Go 环境(go.dev/dl),使用 VS Code 或 GoLand 编写代码,并熟练使用 go run、go build、go mod 等命令。

全部章节目录(共 30 个知识点)

Go 基础 (1个知识点)

接口(interface) (1个知识点)

指针 (1个知识点)

错误处理 (1个知识点)

包管理 (1个知识点)

并发:goroutine (1个知识点)

并发:channel (1个知识点)

同步原语 (1个知识点)

网络与 HTTP (1个知识点)

测试与性能 (1个知识点)

反射与泛型 (1个知识点)

CGO 与汇编 (1个知识点)

Web 框架 (1个知识点)

数据库操作 (1个知识点)

配置与日志 (1个知识点)

部署与运维 (1个知识点)

使用说明

  • • 本手册涵盖 Go 语言从基础到并发实战、Web 服务的完整知识体系
  • • 在搜索框输入关键词,快速定位相关知识点
  • • 点击条目标题进入详情页,查看详细说明与代码示例
  • • 页面按分类展示所有知识点,方便系统性学习
  • • 建议安装 Go 环境并使用 VS Code 或 GoLand 进行实践