去解析yaml文件 [英] Go parse yaml file

查看:257
本文介绍了去解析yaml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Go解析yaml文件.不幸的是我不知道怎么做.我拥有的Yaml文件是这样的:

I'm trying to parse a yaml file with Go. Unfortunately I can't figure out how. The yaml file I have is this:

---
firewall_network_rules:
  rule1:
    src:       blablabla-host
    dst:       blabla-hostname
...

我有此Go代码,但是它不起作用:

I have this Go code, but it does not work:

package main

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

    "gopkg.in/yaml.v2"
)

type Config struct {
    Firewall_network_rules map[string][]string
}

func main() {
    filename, _ := filepath.Abs("./fruits.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", config.Firewall_network_rules)
}

运行此命令时,出现错误.我认为这是因为我尚未为src和dst键/值创建结构.仅供参考:当我将其更改为列表时,它会起作用.

When I run this, I get an error. I think it's because I haven't created a struct for the src and dst key/values. FYI: when I change that to a list, it works.

因此,上面的代码对此进行了解析:

So above code parses this:

---
firewall_network_rules:
  rule1:
    - value1
    - value2
...

推荐答案

如果您使用的是Google Cloud或kubernetes,并且想要解析service.yaml,例如:

If you're working with google cloud or kubernetes more specifically and want to parse a service.yaml like this:

apiVersion: v1
kind: Service
metadata:
  name: myName
  namespace: default
  labels:
    router.deis.io/routable: "true"
  annotations:
    router.deis.io/domains: ""
spec:
  type: NodePort
  selector:
    app: myName
  ports:
    - name: http
      port: 80
      targetPort: 80
    - name: https
      port: 443
      targetPort: 443

提供一个真实的示例,使您了解如何编写嵌套.

Supplying a real world example so you get the hang of how nesting can be written.

type Service struct {
    APIVersion string `yaml:"apiVersion"`
    Kind       string `yaml:"kind"`
    Metadata   struct {
        Name      string `yaml:"name"`
        Namespace string `yaml:"namespace"`
        Labels    struct {
            RouterDeisIoRoutable string `yaml:"router.deis.io/routable"`
        } `yaml:"labels"`
        Annotations struct {
            RouterDeisIoDomains string `yaml:"router.deis.io/domains"`
        } `yaml:"annotations"`
    } `yaml:"metadata"`
    Spec struct {
        Type     string `yaml:"type"`
        Selector struct {
            App string `yaml:"app"`
        } `yaml:"selector"`
        Ports []struct {
            Name       string `yaml:"name"`
            Port       int    `yaml:"port"`
            TargetPort int    `yaml:"targetPort"`
            NodePort   int    `yaml:"nodePort,omitempty"`
        } `yaml:"ports"`
    } `yaml:"spec"`
}

有一个方便的服务,称为yaml-to-go https://yaml.to-go.online/将YAML转换为结构,只需将YAML输入该服务即可获得自动生成的结构.

There's a convenient service called yaml-to-go https://yaml.to-go.online/ which converts YAML to go structs, just input your YAML into that service and you get an autogenerated struct.

最后一位前海报的元帅写道:

And last unmarshal as a previous poster wrote:

var service Service

err = yaml.Unmarshal(yourFile, &service)
if err != nil {
    panic(err)
}

fmt.Print(service.Metadata.Name)

这篇关于去解析yaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆