golang使用Viper的demo

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

获取配置例子

package main

import (
    "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"

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

技术管理之巅

技术管理之巅

黄哲铿 / 电子工业出版社 / 2015-6 / 49.00元

《技术管理之巅——如何从零打造高质效互联网技术团队?》为您解密国内顶级互联网公司技术团队管理的精髓。作者结合自己十余年在国内知名互联网公司MySteel、1号店等担任PMO总监、技术总监的丰富经验,进行归纳和总结。书中围绕着技术管理中的热点“如何搭建扁平化、去中心化的技术团队”、“大数据下的技术管理创新”、“目标管理方法OKR”、“阿米巴生产模式”、“Scrum和Kanban的实践”逐渐展开,从技......一起来看看 《技术管理之巅》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具