当前位置:网站首页>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)
}

 Insert picture description here

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)
}

 Insert picture description here
After modifying the file, press Ctrl+s preservation , such viper To listen for changes in the file

原网站

版权声明
本文为[. fried eggs with tomatoes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100002429315.html