Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report