Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go parse yaml file

Tags:

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 ... 

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) } 

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 ... 
like image 694
Rogier Lommers Avatar asked Feb 23 '15 20:02

Rogier Lommers


People also ask

How do I read a YAML file in Golang?

go mod init example.com/whatever go get gopkg.in/yaml.v3 cat go. sum go run . The tags (like yaml:"field" ) are optional for all-lowercase yaml key identifiers. For shows I've included a random camelCase identifier which does require such a tag.

How do I create a YAML file in Golang?

Example 1: Very basic example of converting a struct to yaml. package main import ( "fmt" "gopkg.in/yaml.v2" ) type Student struct { Name string Age int } func main() { s1 := Student{ Name: "Sagar", Age: 23, } yamlData, err := yaml. Marshal(&s1) if err != nil { fmt.

What is YAML in go?

YAML support for the Go languageThe yaml package enables Go programs to comfortably encode and decode YAML values. It was developed within Canonical as part of the juju project, and is based on a pure Go port of the well-known libyaml C library to parse and generate YAML data quickly and reliably.

What does YAML stand for?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain't markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.


1 Answers

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"` } 

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) 
like image 118
JazzCat Avatar answered Sep 20 '22 22:09

JazzCat