相关疑难解决方法(0)

Golang YAML阅读地图的地图

这是我的YAML文件.

description: fruits are delicious
fruits:
  apple:
    - red
    - sweet
  lemon:
    - yellow
    - sour
Run Code Online (Sandbox Code Playgroud)

我可以用gopkg.in/yaml.v1软件包阅读这个更平的版本,但是当我看到这个YAML文件看起来像是地图的地图时,我一直想知道如何阅读这个YAML文件.

package main

import (
  "fmt"
  "gopkg.in/yaml.v1"
  "io/ioutil"
  "path/filepath"
)

type Config struct {
  Description string
  Fruits []Fruit
}

type Fruit struct {
  Name string
  Properties []string
}

func main() {
  filename, _ := filepath.Abs("./file.yml")
  yamlFile, err := ioutil.ReadFile(filename)

  if err != nil {
    panic(err)
  }

  var config Config

  err = yaml.Unmarshal(yamlFile, &config)
  if err != nil {
    panic(err)
  }

  fmt.Printf("Value: %#v\n", …
Run Code Online (Sandbox Code Playgroud)

yaml go

11
推荐指数
1
解决办法
2万
查看次数

标签 统计

go ×1

yaml ×1