php Editor Banana introduces you to YAML custom tags in Go language. YAML is a lightweight data serialization format, and the Go language, as a powerful programming language, naturally provides support for YAML. In Go, we can define the YAML data structure through custom tags to better parse and process YAML data. By using custom tags, we can easily map YAML data to structures in Go to achieve more flexible and convenient data processing. The following will introduce in detail the usage and precautions of YAML custom tags in Go.
I have these nested structures in go and added custom tags to their properties,
type dummyparams struct { param1 string `yaml:"param1"` param2 string `yaml:"param2"` } type dummy struct { name string `yaml:"name"` type string `yaml:"type"` params dummyparams `yaml:"params"` }
I created some dummy instances and added them to the slice,
dummies := make([]dummy, 0) dummy1 := dummy { name: "a" type: "type a" params: dummyparams { param1: "foo" param2: "bar" } } dummies = append(dummies, dummy1) dummy2 := dummy { name: "b" type: "type b" params: dummyparams { param1: "foo" param2: "bar" } } dummies = append(dummies, dummy2)
Finally I organize the data and write it to a file
yamlData, err := yaml.Marshal(&dummies) // handle error ... writeErr := os.WriteFile("foo.yaml", yamlData, 0644) // handle write error ...
But the yaml I get does not have lowercase tag names but uppercase structure names. Does anyone know why this happens and how to fix it?
Blame it on the yaml implementation you are using. For example, if you use gopkg.in/yaml.v3
it will work. Try it on go playground. So one solution is to use another yaml implementation like gopkg.in/yaml.v3
.
You mentioned in your comments that you are using https:// /pkg.go.dev/sigs.k8s.io/[email protected]. Its package documentation says:
In short, the library first uses go-yaml to convert yaml to json, and then uses json.marshal
and json.unmarshal
to do the conversion with the struct. This means that it effectively reuses the json structure tags as well as the custom json methods marshaljson
and unmarshaljson
, unlike go-yaml.
Sosigs.k8s.io/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c554d41406c5a1d021f021c">[email protected]< /a>
The first one is marshalled to json. If you want lowercase field names, use the json
tag instead of the yaml
tag:
import "sigs.k8s.io/yaml" type dummyparams struct { param1 string `json:"param1"` param2 string `json:"param2"` } type dummy struct { name string `json:"name"` type string `json:"type"` params dummyparams `json:"params"` }
With this change, the output contains lowercase names (try it on go playground):
- name: a params: param1: foo param2: bar type: type a - name: b params: param1: foo param2: bar type: type b
Please note that you must use the json
tag instead of yaml
for this to work just sigs.k8s.io/<a href="/cdn-cgi/ l/email-protection" class="__cf_email__" A quirk of the data-cfemail="047d6569684472352a372a34">[email-protected]</a>
package. If you want it to work with this package and other yaml implementations, you can provide both json
and yaml
tags:
type DummyParams struct { Param1 string `json:"param1" yaml:"param1"` Param2 string `json:"param2" yaml:"param2"` } type Dummy struct { Name string `json:"name" yaml:"name"` Type string `json:"type" yaml:"type"` Params DummyParams `json:"params" yaml:"params"` }
The above is the detailed content of YAML custom tags in Go. For more information, please follow other related articles on the PHP Chinese website!