Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize yaml to map and map traversal in go language

Shulou Source: shulou.com Published: 2022-06-03 10:47:01 09月18日 Update

This article mainly shows you the "go language how to achieve yaml to map, map traversal", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "go language how to achieve yaml to map, map traversal" this article.

Yaml file content

ApiVersion: policy/v1beta1kind: PodSecurityPolicymetadata: name: mysql-snapshotspec: privileged: false allowPrivilegeEscalation: false volumes:-"*" hostNetwork: false hostIPC: false hostPID: false runAsUser: rule: RunAsAny seLinux: rule: RunAsAny supplementalGroups: rule: RunAsAny fsGroup: rule: RunAsAny

Sample code: implementation of base64 codec, yaml to map, and map traversal

Func main () {f, err: = os.Open ("cmd/test.yaml") if err! = nil {log.Fatalln (err) return} input, _: = ioutil.ReadAll (f) / / base64 Encoding encodeString: = base64.StdEncoding.EncodeToString (input) fmt.Println (encodeString) / / base64 Decoding decodeBytes Err: = base64.StdEncoding.DecodeString (encodeString) if err! = nil {log.Fatalln (err)} / / yaml file content converted to map [interface {}] interface {}) resultMap: = make (map [interface {}] interface {}) if err: = yaml.Unmarshal (decodeBytes, & resultMap) Err! = nil {/ / error handling log.Fatalln (err)} / / traversing map Take the metadata.name value for key, value: = range resultMap {switch key: = key. (type) {case string: if key = = "metadata" {switch value: = value. (type) {case string: case map [interface {}] interface {}: for k, v: = range value {switch k. (type) {case string: if k = = "name" {fmt.Println ("value=") V)}}

Result

EncodeString = "YXBpVmVyc2lvbjogcG9saWN5L3YxYmV0YTEKa2luZDogUG9kU2VjdXJpdHlQb2xpY3kKbWV0YWRhdGE6CiAgbmFtZTogbXlzcWwtc25hcHNob3QKc3BlYzogCiAgcHJpdmlsZWdlZDogZmFsc2UKICBhbGxvd1ByaXZpbGVnZUVzY2FsYXRpb246IGZhbHNlCiAgdm9sdW1lczoKICAtICIqIgogIGhvc3ROZXR3b3JrOiBmYWxzZQogIGhvc3RJUEM6IGZhbHNlCiAgaG9zdFBJRDogZmFsc2UKICBydW5Bc1VzZXI6CiAgICBydWxlOiBSdW5Bc0FueQogIHNlTGludXg6CiAgICBydWxlOiBSdW5Bc0FueQogIHN1cHBsZW1lbnRhbEdyb3VwczoKICAgIHJ1bGU6IFJ1bkFzQW55CiAgZnNHcm91cDoKICAgIHJ1bGU6IFJ1bkFzQW55Cg=="

Value=mysql-snapshot

The above is all the content of the article "how to convert yaml to map and map traversal in go language. Thank you for reading!" I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content language articles documents learning help code easy to understand more organization knowledge examples results editing coding industry information information channels channels k. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno vpn Redmi Xiaomi