golang[60]-viper

栏目: Go · 发布时间: 6年前

内容简介:简单的说,viper就是go语言中高效解决配置信息的:Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports:When building a moder

简单的说,viper就是 go 语言中高效解决配置信息的: https://github.com/spf13/viper

What is Viper?

Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports:

  • setting defaults
  • reading from JSON, TOML, YAML, HCL, and Java properties config files
  • live watching and re-reading of config files (optional)
  • reading from environment variables
  • reading from remote config systems (etcd or Consul), and watching changes
  • reading from command line flags
  • reading from buffer
  • setting explicit values
  • Viper can be thought of as a registry for all of your applications configuration needs.

Why Viper?

When building a modern application, you don’t want to worry about configuration file formats; you want to focus on building awesome software. Viper is here to help with that.

Viper does the following for you:

Find, load, and unmarshal a configuration file in JSON, TOML, YAML, HCL, or Java properties formats.

Provide a mechanism to set default values for your different configuration options.

Provide a mechanism to set override values for options specified through command line flags.

Provide an alias system to easily rename parameters without breaking existing code.

Make it easy to tell the difference between when a user has provided a command line or config file which is the same as the default.

Viper uses the following precedence order. Each item takes precedence over the item below it:

explicit call to Set

  • flag
  • env
  • config
  • key/value store
  • default
  • Viper configuration keys are case insensitive.

用例1:读取环境变量信息

绑定变量名为id,前缀spf。则环境变量中设置时必须为SPF_ID

viper.SetEnvPrefix("spf") // 前缀、will be uppercased automatically
viper.BindEnv("id")

os.Setenv("SPF_ID", "13") // typically done outside of the app  export SPF_ID=3000

id := viper.Get("id") // 13
fmt.Println(id)

自动绑定:

 viper.SetEnvPrefix(`spf`)
viper.AutomaticEnv()

id := viper.Get("id")

fmt.Println(id)

用例2:读取配置文件信息

{
  "debug": true,
  "server": {
    "address": ":9090"
  },
  "context":{
    "timeout":2
  },
  "database": {
      "host": "mysql",
      "port": "3306",
      "user": "user",
      "pass": "password",
      "name": "article"
  }

}
package main

import (
	"fmt"
	"github.com/spf13/viper"
)

func main(){

	viper.SetConfigFile(`config.json`)
	err := viper.ReadInConfig()

	if err != nil {
		panic(err)
	}

	if viper.GetBool(`debug`) {
		fmt.Println("Service RUN on DEBUG mode")
	}

	dbHost := viper.GetString(`database.host`)
	dbPort := viper.GetString(`database.port`)
	dbUser := viper.GetString(`database.user`)
	dbPass := viper.GetString(`database.pass`)
	dbName := viper.GetString(`database.name`)
	connection := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPass, dbHost, dbPort, dbName)

	fmt.Println(connection)
}

用例3:命令行参数:

package main


import (
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"fmt"
)

func main(){

	pflag.Int("flagname", 1234, "help message for flagname")

	pflag.Parse()
	viper.BindPFlags(pflag.CommandLine)

	i := viper.GetInt("flagname") // retrieve values from viper instead of pflag
	fmt.Println(i)
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

程序与民主

程序与民主

皮罗·克拉玛德雷 / 翟小波 / 高等教育 / 2005-3 / 8.20元

《程序与民主》是意大利著名政治学家、法学家皮罗·克拉玛德雷(Pierocalamandrei)(1889-1956)讨论现代诉讼程序的著作。该书篇幅虽小,但影响甚大。国内对该著者及其作品的介绍较少,倒是其弟子卡佩莱蒂的著作已有中文译本:《当事人基本程序保障权与未来的民事诉讼》,徐昕译,法律出版社2000年版。《程序与民主》一书,并非平行地讨论程序和民主,而是从程序的视角讨论民主。这里的民主,也不是......一起来看看 《程序与民主》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具