读取文件中的多个Yaml

时间:2018-11-25 02:13:49

标签: go yaml

一个人如何解析文件中的多个Yaml,类似于kubectl的方式?

example.yaml

---
a: Easy!
b:
  c: 0
  d: [1, 2]
---
a: Peasy!
b:
  c: 1000
  d: [3, 4]

3 个答案:

答案 0 :(得分:0)

我使用gopkg.in/yaml.v2找到的解决方案:

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "path/filepath"

    "gopkg.in/yaml.v2"
)

type T struct {
        A string
        B struct {
                RenamedC int   `yaml:"c"`
                D        []int `yaml:",flow"`
        }
}

func main() {
    filename, _ := filepath.Abs("./example.yaml")
    yamlFile, err := ioutil.ReadFile(filename)
    if err != nil {
        panic(err)
    }

    r := bytes.NewReader(yamlFile)

    dec := yaml.NewDecoder(r)

    var t T
    for dec.Decode(&t) == nil {

      fmt.Printf("a :%v\nb :%v\n", t.A, t.B)

    }
}

答案 1 :(得分:0)

gopkg.in/yaml.v2和gopkg.in/yaml.v3之间的行为有所不同:

V2:https://play.golang.org/p/XScWhdPHukO V3:https://play.golang.org/p/OfFY4qH5wW2

两种实现都会产生错误的恕我直言的结果,但V3显然要差一些。

有一个解决方法。如果您在接受的答案中稍稍更改了代码,则它们在yaml软件包的两个版本中均可以正常且以相同的方式工作:https://play.golang.org/p/r4ogBVcRLCb

答案 2 :(得分:0)

Current gopkg.in/yaml.v3 Deocder 产生相当正确的结果,您只需要注意为每个文档创建新结构并检查它是否被正确解析(使用 nil 检查),并处理EOF 错误正确:

package main

import "fmt"
import "gopkg.in/yaml.v3"
import "os"
import "errors"
import "io"

type Spec struct {
    Name string `yaml:"name"`
}

func main() {
    f, err := os.Open("spec.yaml")
    if err != nil {
        panic(err)
    }
    d := yaml.NewDecoder(f)
    for {
        // create new spec here
        spec := new(Spec)
        // pass a reference to spec reference
        err := d.Decode(&spec)
        // check it was parsed
        if spec == nil {
            continue
        }
        // break the loop in case of EOF
        if errors.Is(err, io.EOF) {
            break
        }
        if err != nil {
            panic(err)
        }
        fmt.Printf("name is '%s'\n", spec.Name)
    }
}

测试文件spec.yaml

---
name: "doc first"
---
name: "second"
---
---
name: "skip 3, now 4"
---