[GO language] Let's read a YAML file

What is YAML?

- What is YAML? - How to write yaml (comments, arrays, anchors, aliases)

environment

Preparation

# go get gopkg.in/yaml.v2
# vi test.yml

test.yml


first: test
secound:
  a1: count1
  a2:
  - count2
  - count3
  a3:
    b1: count4

Read YAML file

# vi main.go

main.go


package main

import(
  "fmt"
  "io/ioutil"
  "strconv"
  "gopkg.in/yaml.v2"
)

func main() {
  // test.read yml
  buf, err := ioutil.ReadFile("test.yml")
  if err != nil {
    fmt.Print("error: Failed to read the file\n")
    return
  }

  //Map the read file[interface {}]interface {}Map to
  t := make(map[interface {}]interface {})
  err = yaml.Unmarshal(buf, &t)
  if err != nil {
    panic(err)
  }

  fmt.Print(t["first"])  // test
  fmt.Print("\n")

  // t["secound"]To(map[interface {}]interface {})Type conversion with
  fmt.Print(t["secound"].(map[interface {}]interface {})["a1"])  //count1
  fmt.Print("\n")

  // len()Returns the number of elements in the array
  fmt.Print(len(t["secound"].(map[interface {}]interface {})))
  fmt.Print("\n")

  // []interface {}Array of types
  fmt.Print(t["secound"].(map[interface {}]interface {})["a2"].([]interface {})[0])  // count2
  fmt.Print("\n")

  fmt.Print(t["secound"].(map[interface {}]interface {})["a3"].(map[interface {}]interface {})["b1"]) // count4
  fmt.Print("\n")

  //If it is named regularly, you can check how many there are
  flag, i := 0, 0
  for flag == 0 {
    i++
    switch t["secound"].(map[interface {}]interface {})["a"+strconv.Itoa(i)].(type) {
      case nil:
        flag = 1
    }
  }
  fmt.Printf("a%d is not found\n", i)  // a4 is not found
}
# go run main.go
test
count1
3
count2
count4
a4 is not found

References

- Read and write yaml files with Golang. -<a href=https://medium.com/since-i-want-to-start-blog-that-looks-like-men-do/%E5%88%9D%E5%BF%83%E8% 80% 85% E3% 81% AB% E9% 80% 81% E3% 82% 8A% E3% 81% 9F% E3% 81% 84interface% E3% 81% AE% E4% BD% BF% E3% 81% 84% E6% 96% B9-golang-48eba361c3b4> How to use interface that you want to send to beginners [Golang]

Recommended Posts

[GO language] Let's read a YAML file
Read the config file in Go language! Introducing a simple sample
Read and write a file
Write and read a file
Let's touch on the Go language
Read file
[Note] Read a file from another directory
Let's read the RINEX file with Python ①
Let's make a simple language with PLY 1
Create a VM with a YAML file (KVM)
Read a character data file with numpy
How to read a CSV file with Python 2/3
Create an executable file in a scripting language
Read line by line from a file with Python
How to read a file in a different directory
Create a web server in Go language (net/http) (2)
Create a web server in Go language (net / http) (1)
Go language learning record
Go language environment construction
Read CSV file: pandas
File read overhead measurement
Go language cheat sheet
Features of Go language
Read Python csv file
[Go language] Try to create a uselessly multi-threaded line counter
Python> Read from a multi-line string instead of a file> io.StringIO ()
Let's make a number guessing game in your own language!