当前位置:网站首页>Go profile management -viper
Go profile management -viper
2022-06-10 00:30:00 【. fried eggs with tomatoes】
brief introduction
github
Viper Is applicable to Go Complete configuration solution for applications , It is used to design applications that work , And can handle all types of configuration requirements and formats , It supports the following features :
- Set the default value
- from JSON、TOML、YAML、HCL、envfile and Java properties Profile read
- View and reread the configuration file in real time ( Optional )
- Read from environment variable
- Configure the system remotely (etcd or Consul) Read , And monitor configuration changes
- Read configuration from command line flags
- from buffer Reading configuration
- Explicit configuration value
Quick start
Quick start with YAML For example ;YAML course
YAML
name: test
redis:
host: 127.0.0.1
port: 6379
go
package main
import (
"fmt"
"github.com/spf13/viper"
)
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}
type ServerConfig struct {
Name string `mapstructure:"name"`
RedisConfig RedisConfig `mapstructure:"redis"`
}
func main() {
v := viper.New()
// Path must write relative path , Relative to the path of the project
v.SetConfigFile("viper/demo01/config.yaml")
if err := v.ReadInConfig(); err != nil {
panic(err)
}
// Single value
name := v.Get("name").(string)
// Map to structure
var s ServerConfig
if err := v.Unmarshal(&s);err != nil{
panic(err)
}
fmt.Println(s)
fmt.Println(name)
}

Dynamically monitor changes
package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"time"
)
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}
type ServerConfig struct {
Name string `mapstructure:"name"`
RedisConfig RedisConfig `mapstructure:"redis"`
}
func main() {
v := viper.New()
// Path must write relative path , Relative to the path of the project
v.SetConfigFile("viper/demo02/config.yaml")
if err := v.ReadInConfig(); err != nil {
panic(err)
}
// Map to structure
var s ServerConfig
if err := v.Unmarshal(&s); err != nil {
panic(err)
}
fmt.Println(s)
// Listen for profile changes
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
fmt.Printf(" The configuration file :%s Change \n", in.Name)
if err := v.ReadInConfig(); err != nil {
panic(err)
}
if err := v.Unmarshal(&s); err != nil {
panic(err)
}
fmt.Println(s)
})
// sleep 30 Second, let the function not end immediately ; Allow time to modify the file
time.Sleep(30 * time.Second)
}

After modifying the file, press Ctrl+s preservation , such viper To listen for changes in the file
边栏推荐
- Les contrats à terme Huishang sont - ils formels? La transaction d'ouverture de compte est - elle sécurisée?
- POI exporting Excel
- 2022最新版阿里开发手册发布!!!
- With the advent of the digital era, 360 has joined hands with the dark horse of entrepreneurship to help small and medium-sized enterprises seize the key future
- Composition and configuration of Beidou clock synchronization system in power system
- 数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
- Numpy basic operation
- [typecho] find articles written in non markdown scripting languages
- ADB shell WM command using
- wps合并单元格快捷键是什么
猜你喜欢

电脑连接打印机出现的问题(打印机显示未指定)解决方法

银行有没有必要建立数据中台?看完你就明白了

Pop up frequently: an error occurred when vscode tried to create a file in the target directory. Try again. Skip this file and close setup

SIGIR 2022 | HKU and Wuhan University put forward kgcl: a recommendation system based on knowledge map comparative learning

Py6S配置教程(win10 ×64)

2022最新版阿里开发手册发布!!!

哨兵3(Sentinel-3)数据简介

Q-learning

DDD driven domain design learning notes

Go技术日报(2022-06-08)——聊聊索引失效的10种场景,太坑了
随机推荐
pycharm 2022永久激活版下载,亲测有效
Masao Kudo: how to publish 5 SCI articles a year
【input】 校验只能输入数字
Pta7-5 Sina Weibo hot topics
C # practical skills: package the icon into DLL and read it
numpy基础操作
Process test supports batch parameter import, and the test efficiency is directly full!
MySQL transaction
数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
[typecho] find articles written in non markdown scripting languages
MySQL execution plan
运算符 (第二天)
AI chief architect 5-aica-wenxin NLP large model technology and Application
[typecho] how to query multiple tables
使用打印机打印东西打印不全怎么办
MySQL定时任务(Event Scheduler)
Illustration Google V8 06: prototype chain: how does V8 implement object inheritance?
478. randomly generate points in a circle
[typecho]some problems in SQL programming
低边驱动和高边驱动