当前位置: 首页 > news >正文

Go Viper

Go Viper

什么是viper

  • Go 程序中,处理所有类型的配置需求和格式

  • 处理格式:JSON、YAML等

  • 文档地址:https://github.com/spf13/viper

快速入门

安装
go get github.com/spf13/viper
读取配置
package mainimport ("fmt""github.com/spf13/viper"
)func main() {viper.SetConfigName("config") // 配置文件名viper.SetConfigType("yaml")   // 配置后缀viper.AddConfigPath(".")      // 配置路径err := viper.ReadInConfig()   // Find and read the config fileif err != nil { // Handle errors reading the config filepanic(fmt.Errorf("fatal error config file: %w", err))}fmt.Println("==============取值==============")fmt.Println(viper.AllSettings())  //map[db:map[authsource:admin host:localhost name:test password:root poolsize:10 port:27017 type:mongodb user:root]]fmt.Println(viper.Get("db"))      // map[authsource:admin host:localhost name:test password:root poolsize:10 port:27017 type:mongodb user:root]fmt.Println(viper.Get("db.type")) // mongodb}
动态监听yaml配置文件变更
package mainimport ("fmt""github.com/fsnotify/fsnotify""github.com/spf13/viper"
)func main() {viper.SetConfigName("config") // 配置文件名viper.SetConfigType("yaml")   // 配置后缀viper.AddConfigPath(".")      // 配置路径err := viper.ReadInConfig()   // Find and read the config fileif err != nil { // Handle errors reading the config filepanic(fmt.Errorf("fatal error config file: %w", err))}fmt.Println("==============取值==============")fmt.Println(viper.AllSettings())  //map[db:map[authsource:admin host:localhost name:test password:root poolsize:10 port:27017 type:mongodb user:root]]fmt.Println(viper.Get("db"))      // map[authsource:admin host:localhost name:test password:root poolsize:10 port:27017 type:mongodb user:root]fmt.Println(viper.Get("db.type")) // mongodb// 监听配置文件变化viper.OnConfigChange(func(e fsnotify.Event) {fmt.Println("Config file changed:", e.Name)fmt.Println("新配置值:", viper.AllSettings())})viper.WatchConfig()// 通过一个永远不会收到数据的 channel 阻塞主 goroutinedone := make(chan struct{})<-done // <-done 会一直阻塞,等待从 done channel 接收数据。
}

image-20250323220813275

http://www.jsqmd.com/news/482498/

相关文章:

  • 鸽姆智库全球AI大模型14项核心弊端全维度诊断与根治性解决方案总报告
  • 量化交易系列(七):为什么所有公开的量化策略,都赚不了钱?
  • 【YOLO26实战全攻略】09——YOLO26多目标跟踪实战宝典:从原理到智慧园区人流统计全流程
  • Go Gorm
  • 拒绝 500 与 404:Spring Boot 全局异常处理机制深度解析与常见 API 错误避坑指南
  • 大模型的“大脑”是如何构造的?深度拆解语义建模的三种典型架构
  • 从参数校验失败到序列化陷阱:构建健壮 Spring Boot RESTful API 的十大高频错误复盘
  • 玩转二叉树
  • Thinkphp和Laravel框架都支持 博物馆文物科普知识普及系统微信小程序-
  • Thinkphp和Laravel框架都支持微信小程序的展会展馆纪念馆门票在线预约管理系统19rtj
  • Thinkphp和Laravel框架都支持微信小程序的校园外卖系统 商家
  • Thinkphp和Laravel框架都支持心血管疾病风险预测小程序设计与实现-
  • Thinkphp和Laravel框架都支持微信小程序的校园社区报修上门维修系统
  • 网络安全、计算机网络、理论技术+企业级的产品实践经验相结合Part1 网络安全产品终端侦测与响应系统(EDR)网络侦测与响应系统(NDR)多引擎脆弱性(漏洞)扫描(VAS)网络安全威胁情报
  • 10个成功案例:AI应用架构师是如何用AI激活元宇宙商业生态的?
  • HashMap扩容机制
  • 更新-常用的Flask第三方扩展库清单合集教程和详细的代码示例
  • JavaDays08顺序结构And选择结构
  • 网络安全、渗透测试、安全开发、安全分析岗位面试笔记和参考答案,现已全部更新到服务器
  • HashMap详解
  • AI时代,.NET开发者的生存危机还是能力外挂?
  • 更新-DevOps运维人员必掌握的Linux命令清单教程合集
  • 在1panl安装 skill 比如安装腾讯gp咨询接口 Tushare skills,名称为tushare-data
  • 用mediainfo查看是否是后置mp4
  • 宁夏中宁枸杞品牌都有哪些?玺赞枸杞全维度解析 - 宁夏壹山网络
  • 【Vibe Coding解惑】从 Prompt 到 Code:生成流程解析
  • Godot游戏练习01-第11节-显示优化,游戏背景,Shader
  • 【数学笔记】反演变换
  • 2026春季W2(3.9~3.15)
  • 大语言模型的研究方向