In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.