Phalgo-viper getting the configuration

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

Phalgo-viper getting the configuration

Viper Project Address:Https://github.com/spf13/viper

What is Viper

Viper is an open source configuration solution written by the Great God **spf13** abroad, Viper has the following features and features:

    • Set default values
    • From the Json,toml yaml,hcl, and Java properties configuration file
    • Env read value from environment variable
    • Read buffer
    • To read a configuration file remotely
    • Key is case insensitive

Why use Viper

Viper do not worry about your file format, you can get environment variables, you can get the configuration file from the far end, and there is a buffering mechanism, the function is very good * * * *, to meet the different requirements of the use of the configuration file, so Phalgo use Viper to solve the configuration problem

Initialize Config

Phalgo pursues the simplest use of various components so viper we just need to initialize to get started, we just need to call the **newconfig** function you pass in two parameters, one is your file relative to the project directory, such as I created in the project directory Conf file directory needs to fill in the Conf, the second is the profile file name, only need to name, you can not enter the suffix Viper will be automatically recognized.

//初始化配置文件phalgo.NewConfig("conf", "sys")

For example, we created a **sys.toml** file with the following contents:

It is highly recommended to use TOML format, toml format description: http://mlworks.cn/posts/introduction-to-toml/

[system]port = ":1234"[dbDefault]dbHost = "localhost"           #数据库连接地址dbName = "phalgo"              #数据库名称dbUser = "root"                #数据库用户名dbPasswd = ""                  #数据库密码dbPort = "3306"                #数据库端口号dbOpenconns_max = 20           #最大连接数dbIdleconns_max = 0            #最大空闲连接dbType = "mysql"               #数据库类型

We can use it simply.

phalgo.Config.GetString("system.port")  #返回一个string类型的":1234"

Phalgo. Config and * * "github.com/spf13/viper" * * are equivalent, so you can pass Phalgo. Config to invoke the method provided by Viper

Config detailed

Set default values

phalgo.Config.SetDefault("ContentDir", "content")phalgo.Config.SetDefault("LayoutDir", "layouts")phalgo.Config.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})

Get different types of configurations

    • Phalgo. Config.get (Key string): interface{}
    • Phalgo. Config.getbool (Key string): BOOL
    • Phalgo. Config.getfloat64 (Key string): Float64
    • Phalgo. Config.getint (Key string): int
    • Phalgo. Config.getstring (Key string): string
    • Phalgo. Config.getstringmap (Key string): map[string]interface{}
    • Phalgo. Config.getstringmapstring (Key string): Map[string]string
    • Phalgo. Config.getstringslice (Key string): []string
    • Phalgo. Config.gettime (Key string): time. Time
    • Phalgo. Config.getduration (Key string): time. Duration
    • Phalgo. Config.isset (Key string): BOOL

Get Multilevel parameters

Viper supports getting the hierarchical relationship of the configuration, passing "." Between each key. Segmentation mechanisms, such as those shown above

phalgo.Config.GetString("system.port")

is to get the level two parameter

More

Viper offers a number of interesting features that can be learned by opening the Viper project address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.