Add yaml-unmarshal examples

[?]
Dec 27, 2020, 9:09 PM
KGSQ3OPPOY27FODPPPNFANPK2QL462RVTQIX6DISNR2X5GIGEGUAC

Dependencies

Change contents

  • file addition: yaml-unmarshal (dxwrx-rx-r)
    [4.11]
  • file addition: main.go (-xw-x--x--)
    [0.17]
    package main
    import (
    "fmt"
    "log"
    yaml "gopkg.in/yaml.v2"
    )
    func main() {
    input1 := `
    - path: /foo
    url: https://hup.hu
    - path: /whatevs
    url: http://index.hu
    `
    // Convert YAML to struct
    type yamlStruct struct {
    Path string
    URL string
    }
    var test1 []yamlStruct
    err := yaml.Unmarshal([]byte(input1), &test1)
    if err != nil {
    log.Printf("%s", err)
    }
    fmt.Println(test1)
    input2 := `
    entries:
    - path: /foo
    url: https://hup.hu
    - path: /whatevs
    url: http://index.hu
    `
    // Convert YAML to map[string]interface{}
    // This will create separate map for each entry
    type IntEntry map[string]interface{}
    type mapStruct struct {
    Element []IntEntry `yaml:"entries"`
    }
    var test2 mapStruct
    err = yaml.Unmarshal([]byte(input2), &test2)
    if err != nil {
    log.Printf("%s", err)
    }
    fmt.Println(test2)
    }
  • replacement in snippets/tictactoe/go.mod at line 5
    [2.2004][2.2004:2047]()
    require github.com/stretchr/testify v1.6.1
    [2.2004]
    require (
    github.com/stretchr/testify v1.6.1
    golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e // indirect
    )