取付
go get github.com/viper
簡単な例
/*
- main.go
- config.yaml
*/
func main(){
viper.SetConfigName("config") // コンフィギュレーション名を設定する
viper.SetConfigFile("config.yaml") // コンフィギュレーションファイルのパスを設定する
// コンフィグを読む
if err := viper.ReadInConfig(); err != nil{
log.Fatal(err)
}
// コンフィギュレーションデータを取得する
summary := viper.GetString("Summary")
fmt.Println(summary)
}
ニューバイパー
viperは、直接使用できるデフォルトのViperオブジェクトを提供します。 カスタムバイパーは、Newメソッドで作成することもできます。
// デフォルトのオブジェクトを直接使う
viper.GetInt("count")
// グローバルViperオブジェクトを取得する
globalViper := viper.GetViper()
// Viper
conf := viper.New()
conf.SetConfigFile("config.yaml")
設定ファイルの読み込み
コンフィギュレーションからの読み込み
viper.SetConfigName("config")
viper.SetConfigFile("config.yaml")
if err := viper.ReadInConfig(); err != nil{
log.Fatal(err)
}
version := viper.GetString("version")
ioからお読みください。
func main(){
file, err := os.Open("./config.yaml")
if err != nil{
log.Fatal(err)
}
defer file.Close()
viper.SetConfig("yaml")
version := viper.GetString("version")
// ここでコンフィギュレーション・タイプを設定する必要がある。
// コンフィギュレーション・タイプを持っていない場合は、Getメソッドからデータを取得し、自分でパースすることもできる。
}
フラグからの読み取り
import (
"fmt"
"github.com/pflag"
"github.com/viper"
)
func main() {
pflag.String("ip", ".1", "Server running address")
pflag.Int64("port", 8080, "Server running port")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
fmt.Printf("ip :%s , port:%s", viper.GetString("ip"), viper.GetString("port"))
}
値をとる
Get[type]という形式のメソッドを用意する価値があります。
- ブール値
- GetBool
- 時間
- GetDuration
- GetTime
- デュレーションの取得
- 所要時間
- GetInt
- 番号
- GetInt
- GetInt32
- GetInt64
- GetFloat64
- GetIntSlice
- GetStringSlice
デフォルト値の設定
セットデフォルト
viper.setConfigFile("config.yaml")
viper.SetDefault("port", 8000)
println(viper.GetInt("port") // 8000
// コンフィギュレーション・ファイルが読み込まれなかった場合、デフォルト値が返される。
コンフィギュレーションの構造へのマッピング
- Unmarshalは、JSONの解析に似たマッチングパターンで、大文字で始まる属性にのみマッチするように、構成属性を構造体にマッピングします。
type User struct{
ID string
Name string
nickName string // 小文字の属性はマッチしない
}
func main(){
/* config.yaml
id: id11
name: Rogan
nickName: "wolf"
*/
...
user := &User{}
viper.Unmarshal(user)
println(&user) // { id11, Rogan }
}
- UnmarshalExactはUnmarshalと同じように使用されますが、構造体が構成属性と正確に一致する必要があります。
/*
config.yaml
id: 111
name: Rogan
*/
type Conf_1 struct{
Id string
Name string
}
//
type Conf_2 struct{
Id string
}
- UnmarshalKey はフィールドにマッチします。
/*
config.yaml
server:
port: 8000
host: localhost
*/
type ServerConf struct{
Port int
host string
}
func main(){
...
serverConf := &ServerConf{}
viper.UnmarshalKey("server", serverConfig)
}
混合コンフィギュレーション Merge[type]
- MergeConfig
func main(){
viper.SetConfigName("config")
viper.SetConfigFile("config.yaml")
viper.ReadInConfig()
log.Println(viper.GetString("Summary"), viper.InConfig("Summary"))
buff := []byte(`Name: jeck`)
viper.MergeConfig(bytes.NewReader(buff))
log.Println(viper.GetString("Name"))
}
ファイルの試聴
- WatchConfig
func main(){
viper.SetConfigName("config")
viper.SetConfigFile("config.yaml")
viper.ReadInConfig()
viper.WatchConfig()
for {
log.Println(viper.GetString("version"))
time.Sleep(time.Second * 5)
}
}
// versioni: 1
// --> 1
// config.yaml version: 2
// --> 2
- OnConfigChange 設定変更に応答




