Golang Using the Viper Demo

Source: Internet
Author: User

Get Configuration examples

package mainimport (    "flag"    "fmt"    "runtime"    "github.com/spf13/viper")type Config struct {    Base BaseConf `mapstructure:"base"`}type BaseConf struct {    pidfile   string `mapstructure:"pidfile"`}var (    Conf     *Config    confPath string)func init() {    flag.StringVar(&confPath, "d", "./", " set logic config file path")}func InitConfig() (err error) {    Conf = NewConfig()    viper.SetConfigName("logic") // 配置文件的名字    viper.SetConfigType("toml") // 配置文件的类型    viper.AddConfigPath(confPath) // 配置文件的路径    if err := viper.ReadInConfig(); err != nil {        return err    }    if err := viper.Unmarshal(&Conf); err != nil {        panic(fmt.Errorf("unable to decode into struct:  %s \n", err))    }    return nil}func NewConfig() *Config {    return &Config{        Base: BaseConf{            Pidfile:   "/tmp/comet.pid",        },    }}# toml 文件内容为[base]pidfile = "/tmp/logic.pid"
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.