在 Go 中解析 YAML 文件
问题:
您在解析Go 中的 YAML 文件。下面是您尝试解析的示例 YAML 文件:
--- firewall_network_rules: rule1: src: blablabla-host dst: blabla-hostname ...
解决方案:
要解析给定的 YAML 文件,您需要创建一个结构体准确地反映其结构。在您的情况下,YAML 文件包含嵌套元素,因此您的结构应该镜像该嵌套。正确的结构定义是:
type FirewallNetworkRule struct { Src string `yaml:"src"` Dst string `yaml:"dst"` } type Config struct { FirewallNetworkRules map[string][]FirewallNetworkRule `yaml:"firewall_network_rules"` }
现在,要将 YAML 文件解组到 Config 结构中,请使用以下代码:
var config Config err := yaml.Unmarshal(yamlFile, &config) if err != nil { panic(err) } fmt.Printf("Value: %#v\n", config.FirewallNetworkRules)
高级示例:
使用复杂的 YAML 文件(例如 Kubernetes 或 Google Cloud 服务 YAML)时,您可能需要嵌套结构来表示复杂的数据结构。例如,以下 YAML 定义了 Kubernetes 服务:
apiVersion: v1 kind: Service metadata: name: myName namespace: default labels: router.deis.io/routable: "true" annotations: router.deis.io/domains: "" spec: type: NodePort selector: app: myName ports: - name: http port: 80 targetPort: 80 - name: https port: 443 targetPort: 443
此 YAML 对应的 Go 结构将是:
type Service struct { APIVersion string `yaml:"apiVersion"` Kind string `yaml:"kind"` Metadata struct { Name string `yaml:"name"` Namespace string `yaml:"namespace"` Labels struct { RouterDeisIoRoutable string `yaml:"router.deis.io/routable"` } `yaml:"labels"` Annotations struct { RouterDeisIoDomains string `yaml:"router.deis.io/domains"` } `yaml:"annotations"` } `yaml:"metadata"` Spec struct { Type string `yaml:"type"` Selector struct { App string `yaml:"app"` } `yaml:"selector"` Ports []struct { Name string `yaml:"name"` Port int `yaml:"port"` TargetPort int `yaml:"targetPort"` NodePort int `yaml:"nodePort,omitempty"` } `yaml:"ports"` } `yaml:"spec"` }
要将 YAML 文件解组到此结构中,请使用以下代码:
var service Service err := yaml.Unmarshal(yourFile, &service) if err != nil { panic(err) } fmt.Print(service.Metadata.Name)
以上是如何在 Go 中有效解析复杂的 YAML 文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

goroutinesarefunctionsormethodsthatruncurranceingo,启用效率和灯威量。1)shememanagedbodo'sruntimemultimusingmultiplexing,允许千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函数可以从panic中恢复。具体方法是:1)在defer函数中使用recover()捕获panic,避免程序崩溃;2)记录详细的错误信息以便调试;3)根据具体情况决定是否恢复程序执行;4)谨慎使用,以免影响性能。

本文讨论了使用GO的“字符串”软件包进行字符串操作,详细介绍了共同的功能和最佳实践,以提高效率并有效地处理Unicode。

本文详细介绍了GO的“时间”包用于处理日期,时间和时区,包括获得当前时间,创建特定时间,解析字符串以及测量经过的时间。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

禅工作室 13.0.1
功能强大的PHP集成开发环境