Home > Backend Development > Golang > How do I Parse YAML Files in Go?

How do I Parse YAML Files in Go?

Linda Hamilton
Release: 2024-11-29 06:07:12
Original
573 people have browsed it

How do I Parse YAML Files in Go?

Parsing YAML Files in Go

Parsing YAML files in Go requires understanding the structure of the data and the appropriate data types to represent it.

Example: Parsing a Firewall Network Rules File

Consider the following YAML file with firewall network rules:

---
firewall_network_rules:
  rule1:
    src:       blablabla-host
    dst:       blabla-hostname
...
Copy after login

To parse this file, we'll define a Config struct to represent the YAML contents:

type Config struct {
    Firewall_network_rules map[string][]string
}
Copy after login

We'll then use the yaml package to unmarshal the YAML file into the Config struct:

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)
}
Copy after login

This approach works because the YAML file uses a nested map structure that corresponds to the Config struct.

Example: Parsing a Service YAML File

To parse a more complex YAML file like a Kubernetes service manifest, we'll create a more complex struct:

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"`
}
Copy after login

We'll then unmarshal the YAML file into this struct:

var service Service

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

fmt.Print(service.Metadata.Name)
Copy after login

By using appropriate structs that match the YAML structure, we can effectively parse and represent complex YAML data in Go.

The above is the detailed content of How do I Parse YAML Files in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template