liveBook · Manning

YAML

As explained in Go in Practice, Second Edition

YAML is a human-readable data serialization format commonly used for structured configuration and data exchange.

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

How Go in Practice, Second Edition covers it

How deep it goes
Defines YAML and demonstrates file parsing plus typed value access, but defers broader processor details.
Examples
Shows commented path and enabled settings alongside a Go program that reads and prints them.
What it assumes
Assumes basic Go and configuration-file familiarity while introducing YAML's purpose and library-based usage.
What you can do after
Operate/configure a Go application by reading YAML files and retrieving string and Boolean settings.
How it connects
Contrasts YAML with JSON and INI while connecting it to Go configuration files and third-party libraries.

Also explained in

Compare how 16 books explain YAML →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.