config: github.com/gookit/config/yaml Index | Examples | Files

package yaml

import "github.com/gookit/config/yaml"

Package yaml is a driver use YAML format content as config source

Usage please see example:

Code:

config.WithOptions(config.ParseEnv)

// add yaml decoder
// only add decoder
// config.SetDecoder(config.Yaml, Decoder)
// Or
config.AddDriver(Driver)

err := config.LoadFiles("../testdata/yml_other.yml")
if err != nil {
    panic(err)
}

// load from string
_ = config.LoadSources(config.Yaml, []byte(yamlStr))

fmt.Print("get config example:\n")

name := config.String("name")
fmt.Printf("get string\n - val: %v\n", name)

arr1 := config.Strings("arr1")
fmt.Printf("get array\n - val: %#v\n", arr1)

val0 := config.String("arr1.0")
fmt.Printf("get sub-value by path 'arr.index'\n - val: %#v\n", val0)

map1 := config.StringMap("map1")
fmt.Printf("get map\n - val: %#v\n", map1)

val0 = config.String("map1.key")
fmt.Printf("get sub-value by path 'map.key'\n - val: %#v\n", val0)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// Out:
// get config example:
// age: 23
// get string
// - val: app2
// get array
// - val: []string{"val1", "val21"}
// get sub-value by path 'arr.index'
// - val: "val1"
// get map
// val: map[string]string{"key":"val2", "key2":"val20"}
// get sub-value by path 'map.key'
// - val: "val2"
// get env 'envKey' val: /bin/zsh
// get env 'envKey1' val: defValue

Index

Examples

Package Files

yaml.go

Variables

var Decoder config.Decoder = yaml.Unmarshal

Decoder the yaml content decoder

var Driver = &yamlDriver{config.Yaml}

Driver for yaml

var Encoder config.Encoder = yaml.Marshal

Encoder the yaml content encoder

Package yaml imports 2 packages (graph) and is imported by 2 packages. Updated 2021-01-26. Refresh now. Tools for package owners.