SoFunction
Updated on 2025-03-04

golang parses yaml file operation

First install the parsed third-party package:

go get /yaml.v2

Example:

package main 
import (
 "os"
 "log"
 "fmt"
 "encoding/json" 
 "/yaml.v2"
)
 
type Config struct {
 Test Test `yaml:"test"`
}
 
type Test struct {
 User []string `yaml:"user"`
 MQTT MQ `yaml:"mqtt"`
 Http HTTP `yaml:"http"`
}
 
type HTTP struct {
 Port string `yaml:"port"`
 Host string `yaml:"host"`
} 
 
type MQ struct {
 Host string `yaml:"host"`
 Username string `yaml:"username"`
 Password string `yaml:"password"`
} 
 
//read yaml config
//Note: path is the path of yaml or yml filefunc ReadYamlConfig(path string) (*Config,error){
 conf := &Config{}
 if f, err := (path); err != nil {
  return nil,err
 } else {
 (f).Decode(conf)
 }
 return conf,nil
}
 
//test yaml
func main() {
 conf,err := ReadYamlConfig("D:/test_yaml/")
 if err != nil {
 (err)
 }
 
 byts,err := (conf)
 if err != nil {
 (err)
 }
 
 (string(byts))
}
 

The content is as follows:

test:
 user:
 - Tom
 - Lily
 - Skay
 
 mqtt:
  host: localhost:1883
  username: test
  password: test
 
 http: {port: "8080", host: "127.0.0.1"}

Running results:

{"Test":{"User":["Tom","Lily","Skay"],"MQTT":{"Host":"localhost:1883","Username":"test","Password":"test"},"Http":{"Port":"8080","Host":"127.0.0.1"}}}

Supplement: golang reads yml format, multi-structure cascade

1. Install the yml parsing package

Go to the gopath to execute the command

go get /yaml.v2

Source code address/go-yaml/yaml

2. Set configuration file

ipport: 192.168.2.95:10000
startsendtime: 2017-01-02 08:08:08
sendmaxcountperday: 100
devices:
- devid: 123456789
 nodes:
 - pkid: 0
  bkid: 0
  index: 0
  minvalue: 0
  maxvalue: 60
  datatype: normal
 - pkid: 0
  bkid: 0
  index: 0
  datatype: boolean
- devid: 10001
 nodes:
 - pkid: 0
  bkid: 1
  index: 0
  datatype: boolean
warnfrequency: 10
sendfrequency: 10

3. Write test classes

package main
import (
  "fmt"
  "/yaml.v2"
  "io/ioutil"
)

//The letters in the configuration file should be lowercase, and the first letter of the structure attribute should be uppercase
type Myconf struct {
  Ipport  string
  StartSendTime string
  SendMaxCountPerDay int
  Devices []Device
  WarnFrequency int
  SendFrequency int
}
type Device struct {
  DevId string
  Nodes []Node
}
type Node struct {
  PkId string
  BkId string
  Index string
  MinValue float32
  MaxValue float32
  DataType string
}

func main() {
  data, _ := ("")
  (string(data))
  t := Myconf{}
  //Parse yaml strings into struct type  (data, &t)
  ("Initial Data", t)
  if(==""){
    ("Profile setting error")
    return;
  }
  d, _ := (&t)
  ("have a look :", string(d))
}

4. Pay attention

1. The letters in the configuration file should be lowercase, and the first letter of the structure attribute should be uppercase, so the development is faster.

You can also specify such as: yaml:"c", but it's a bit troublesome. Of course, if you rename it, you must specify it.

:",flow"

This means to display the array in a format like ["a","b"]. The default display form is

- a

- b

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.