kubernetes-sigs/yaml is a permanent fork of ghodss/yaml.
A wrapper around go-yaml designed to enable a better way of handling YAML when marshaling to and from structs.
In short, this library first converts YAML to JSON using go-yaml and then uses json.Marshal
and json.Unmarshal
to convert to or from the struct. This means that it effectively reuses the JSON struct tags as well as the custom JSON methods MarshalJSON
and UnmarshalJSON
unlike go-yaml. For a detailed overview of the rationale behind this method, see this blog post.
This package uses go-yaml and therefore supports everything go-yaml supports.
Caveat #1: When using yaml.Marshal
and yaml.Unmarshal
, binary data should NOT be preceded with the !!binary
YAML tag. If you do, go-yaml will convert the binary data from base64 to native binary data, which is not compatible with JSON. You can still use binary in your YAML files though - just store them without the !!binary
tag and decode the base64 in your code (e.g. in the custom JSON methods MarshalJSON
and UnmarshalJSON
). This also has the benefit that your YAML and your JSON binary data will be decoded exactly the same way. As an example:
BAD: exampleKey: !!binary gIGC GOOD: exampleKey: gIGC ... and decode the base64 data in your code.
Caveat #2: When using YAMLToJSON
directly, maps with keys that are maps will result in an error since this is not supported by JSON. This error will occur in Unmarshal
as well since you can‘t unmarshal map keys anyways since struct fields can’t be keys.
To install, run:
$ go get sigs.k8s.io/yaml
And import using:
import "sigs.k8s.io/yaml"
Usage is very similar to the JSON library:
package main import ( "fmt" "sigs.k8s.io/yaml" ) type Person struct { Name string `json:"name"` // Affects YAML field names too. Age int `json:"age"` } func main() { // Marshal a Person struct to YAML. p := Person{"John", 30} y, err := yaml.Marshal(p) if err != nil { fmt.Printf("err: %v\n", err) return } fmt.Println(string(y)) /* Output: age: 30 name: John */ // Unmarshal the YAML back into a Person struct. var p2 Person err = yaml.Unmarshal(y, &p2) if err != nil { fmt.Printf("err: %v\n", err) return } fmt.Println(p2) /* Output: {John 30} */ }
yaml.YAMLToJSON
and yaml.JSONToYAML
methods are also available:
package main import ( "fmt" "sigs.k8s.io/yaml" ) func main() { j := []byte(`{"name": "John", "age": 30}`) y, err := yaml.JSONToYAML(j) if err != nil { fmt.Printf("err: %v\n", err) return } fmt.Println(string(y)) /* Output: age: 30 name: John */ j2, err := yaml.YAMLToJSON(y) if err != nil { fmt.Printf("err: %v\n", err) return } fmt.Println(string(j2)) /* Output: {"age":30,"name":"John"} */ }
洋葱什么时候种植 | 梵音是什么意思 | 老虎菜为什么叫老虎菜 | 肌腱炎吃什么药 | 吃什么可以护肝养肝 |
人体含量最多的元素是什么 | ab型和b型生的孩子是什么血型 | 光子嫩肤是什么 | cdfl是什么意思 | 紫癜吃什么药 |
杳冥是什么意思 | 什么是房补 | 什么的小船 | 为什么会气虚 | 缺钙吃什么 |
澍在人名中读什么 | exo的e为什么不发音 | 电动伐木锯什么牌子好 | 生物工程学什么 | 梦见手机摔碎了是什么意思 |
月经颜色发黑是什么原因hcv8jop2ns5r.cn | 狮子座是什么象星座hcv9jop2ns7r.cn | 跑步的配速是什么意思hcv7jop5ns1r.cn | 贫血吃什么东西好hcv8jop2ns8r.cn | 前轮轴承坏了会有什么症状hcv8jop9ns3r.cn |
加湿器有什么用hcv8jop9ns4r.cn | 卡司是什么意思hcv9jop2ns2r.cn | 有人的地方就有江湖什么意思hcv9jop6ns7r.cn | 双肺局限性气肿是什么病hcv9jop1ns9r.cn | 医美是什么专业hcv9jop6ns7r.cn |
仔字五行属什么chuanglingweilai.com | 执业药师是干什么的xjhesheng.com | 苏州古代叫什么hcv8jop4ns2r.cn | 男生属鸡和什么属相配creativexi.com | 晚点是什么意思weuuu.com |
心肌酶高是什么意思hcv8jop4ns6r.cn | 体细胞是什么意思ff14chat.com | 寅时是什么时间gysmod.com | 梦见棺材是什么意思hcv9jop4ns9r.cn | 生石灰是什么hcv8jop0ns4r.cn |