YAML is a human-readable data serialization format suited to application configuration. It supports comments, remains easy to read, and avoids some of JSON's rigidity. Go does not include a YAML processor, so applications use a third-party library. section 2.2.1
Reading a YAML configuration
A Go program can read conf.yaml with github.com/kylelemons/go-gypsy/yaml, retrieve path as a string, retrieve enabled as a Boolean, and return early when the file or required setting is invalid. section 2.2.1
# A comment line enabled: true path: /usr/local
This configuration demonstrates YAML comments and simple key-value settings. section 2.2.1
Listing 2.10 Parsing a YAML configuration file
package main
import (
"fmt"
"github.com/kylelemons/go-gypsy/yaml" #1
)
func main() {
config, err := yaml.ReadFile("conf.yaml") #2
if err != nil {
fmt.Println(err)
return
}
var path string
var enabled bool
path, err = config.Get("path")
if err != nil {
fmt.Println("`path` flag not set in conf.yaml", err)
return
}
enabled, err = config.GetBool("enabled")
if err != nil {
fmt.Println("`enabled` flag not set in conf.yaml", err)
return
}
fmt.Println("path", path) #3
fmt.Println("enabled", enabled) #3
}
This runnable example shows file loading, string and Boolean retrieval, validation, and output. section 2.2.1















