美文网首页
spf13 viper代码示例

spf13 viper代码示例

作者: 莫名FCJ | 来源:发表于2017-11-03 11:01 被阅读1927次

代码

https://github.com/fengchunjian/goexamples/viper

//main.go
package main

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

const cmdRoot = "core"

func main() {
    viper.SetEnvPrefix(cmdRoot)
    viper.AutomaticEnv()
    replacer := strings.NewReplacer(".", "_")
    viper.SetEnvKeyReplacer(replacer)
    viper.SetConfigName(cmdRoot)
    viper.AddConfigPath("./")

    err := viper.ReadInConfig()
    if err != nil {
        fmt.Println(fmt.Errorf("Fatal error when reading %s config file:%s", cmdRoot, err))
        os.Exit(1)
    }

    environment := viper.GetBool("security.enabled")
    fmt.Println("security.enabled:", environment)

    fullstate := viper.GetString("statetransfer.timeout.fullstate")
    fmt.Println("statetransfer.timeout.fullstate:", fullstate)

    abcdValue := viper.GetString("peer.abcd")
    fmt.Println("peer.abcd:", abcdValue)
}

//core.yaml
statetransfer:
    recoverdamage: true
    blocksperrequest: 20
    maxdeltas: 200
    timeout:
        singleblock: 2s
        singlestatedelta: 2s
        fullstate: 60s
peer:
    abcd:   3322d

编译

go get github.com/spf13/viper
go build -o vip

处理go get github.com/spf13/viper过程中问题

unrecognized import path "golang.org/x/sys/unix"
unrecognized import path "golang.org/x/text/transform"
unrecognized import path "golang.org/x/text/unicode/norm"
解决办法:
git clone https://github.com/golang/sys.git $GOPATH/src/golang.org/x/sys
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text

运行

CORE_SECURITY_ENABLED=true ./vip
security.enabled: true
statetransfer.timeout.fullstate: 60s
peer.abcd: 3322d

参考文档

golang插件viper
http://blog.csdn.net/qq_27809391/article/details/54091977

相关文章

  • spf13 viper代码示例

    代码 https://github.com/fengchunjian/goexamples/viper 编译 go...

  • go viper包

    什么是viper viper大神spf13编写的开源配置解决方案,viper拥有以下功能以及特性: 1、设置默认值...

  • spf13/viper

    参考资料 仓库地址 https://github.com/spf13/viper[https://github.c...

  • spf13 cobra代码示例

    代码 https://github.com/fengchunjian/goexamples/cobra 编译 go...

  • go-viper库-配置文件

    项目地址 https://github.com/spf13/viper 支持的功能 设置默认值 从本地的JSON,...

  • 使用Viper示例

    1、假设我们的项目现在有一个./conf/config.yaml配置文件,内容如下: 2、直接使用viper管理配...

  • 电子档案管理系统单点登陆示例

    JAVA示例 前台示例代码 后台示例代码 .NET示例 前台ASPX示例代码 后台CS示例代码

  • day12-json文件和异常处理

    1.文件操作 代码示例 代码示例 2.json文件(重要) 代码示例 运行结果 代码示例 代码示例 运行结果 代码...

  • 10.15 day12 json文件和异常处理

    1.文件操作 代码示例 代码示例 2.json文件(重要) 代码示例 运行结果 代码示例 代码示例 运行结果 代码...

  • 05-18golang中pflag

    参考github spf13/pflag[https://github.com/spf13/pflag]Golan...

网友评论

      本文标题:spf13 viper代码示例

      本文链接:https://www.haomeiwen.com/subject/tattmxtx.html