Yaml file name:
agent: agentName: agent001 location: type: cdb env: prod role: ro set: tsh2 idc: st4 zone: ap-shanghai-4 hostname: content: register new Agent agent001
Note, do not use the tab key
golang file:
package main import ( "fmt" "os" "/yaml.v2" ) type LocationKey struct { CloudType string `yaml:"type"` Env string `yaml:"env"` Role string `yaml:"role"` Set string `yaml:"set"` Idc string `yaml:"idc"` Zone string `yaml:"zone"` Hostname string `yaml:"hostname"` } type Agent struct { AgentName string `yaml:"agentName"` AgentKey LocationKey `yaml:"location"` Content string `yaml:"content"` } type as struct { Magent Agent `yaml:"agent"` } func main() { var a as data, err := ("./") if err != nil { (()) return } (string(data)) err = (data, &a) if err != nil { (()) return } (a) }
implement:
# go mod tidy # go build
This is the article about this example code of golang reading yaml files. For more related golang reading yaml files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!