SoFunction
Updated on 2025-03-05

Golang parsing yaml file operation guide

Preface

yamlFiles are the most commonly used configuration files for R&D personnel.yamlThe tree structure of files has always been very popular. HaveSpringBootStudents with experience in developmentyamlVery familiar,SpringBootThe entire project needs to run oneFile support, then the Golang projectyamlHow do files parse? Let`s dive in!

PS: According togodocsTo put it, Golang has three powerful toolkits to supportyamlThe parsing of files is:go-gypsy go-yaml goccy-yaml. We will discuss this in this articlego-yamlHow to use.

Students who are interested in yaml parsing source code, please enter:go-yaml source code link

Simple Demo

In the first step, after creating the project, we import the go-yaml dependency:

➜  go-yaml go get /yaml.v3
go: added /yaml.v3 v3.0.1

The second step is to create the main file and write a simple structure internally:

type ConfDemo struct {
  // The following yaml annotation is the attribute name in the yaml file	A int      `yaml:"a"`
	B string   `yaml:"b"`
	C bool     `yaml:"c"`
	D []string `yaml:"d"`
	E struct {
		EA string `yaml:"ea"`
		EB string `yaml:"eb"`
	} `yaml:"e"`
}

The third step is to create the conf directory in the home directory and create the conf_demo.yaml file in the conf directory to write our configuration:

a: 1
b: "I am B"
c: true
d:
  - "I"
  - "am"
  - "D"
e:
  ea: "I am EA"
  eb: "I am EB"

Step 4: Write the main function:

func main() {
  // Read all the contents of the file and load them into []byte	bytes, err := ("config/conf_demo.yaml")
	if err != nil {
		(err)
	}
  // Create a configuration file structure	var confDemo ConfDemo
  // Call Unmarshall to decode the file contents  // Be careful to enter through the pointer of the configuration structure	err = (bytes, &confDemo)
	if err != nil {
		(err)
	}
  // Call Unmarshall to encode the decoded confDemo  // The returned yml is of type []byte  yml, err := (confDemo)
	if err != nil {
		(err)
	}
  // Output result	("%#v\n", confDemo)
  ("%s\n", yml)
}

Step 5, run and view the results:

➜  go-yaml go run 
{A:1, B:"I am B", C:true, D:[]string{"I", "am", "D"}, E:struct { EA string "yaml:\"ea\""; EB string "yaml:\"eb\"" }{EA:"I am EA", EB:"I am EB"}}
a: 1
b: I am B
c: true
d:
    - I
    - am
    - D
e:
    ea: I am EA
    eb: I am EB

go-yaml other parsing methods

The first analytical method is shown in simple demoMarshallandUnmarshallMethods, they operate directly on structures and byte streams. But sometimes, for the convenience of the graph, we want to hand over the step of reading byte streams to the component to perform, and at this time we can useand

andexistandThe operation is performed on reading its byte stream and performing the actions of encoding and decoding. We implement the above example again in this way:

func main() {
  // Use to obtain the File object, which implements and	file, err := ("config/conf_demo.yaml")
	if err != nil {
		(err)
	}
  // Construct a new Decoder and pass in the file	decoder := (file)
  // Configuration file structure	var confDemo ConfDemo
  // Decoding operation, be careful to pass in the address	err = (&confDemo)
  // Output the decoding result	("%#v\n", confDemo)
	if err != nil {
		(err)
	}
  // Construct a new Encoder, which is directly passed in here, which means that the result is directly output to the console	encoder := ()
  // Encoding and outputting	err = (confDemo)
	if err != nil {
		(err)
	}
}

Running results:

➜  go-yaml go run 
{A:1, B:"I am B", C:true, D:[]string{"I", "am", "D"}, E:struct { EA string "yaml:\"ea\""; EB string "yaml:\"eb\"" }{EA:"I am EA", EB:"I am EB"}}
a: 1
b: I am B
c: true
d:
    - I
    - am
    - D
e:
    ea: I am EA
    eb: I am EB

Summarize

This is the article about Golang parsing yaml file operation. For more related Golang parsing yaml file content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!