首页  >  文章  >  后端开发  >  如何在 Go 中解析带有动态键的 YAML 文件?

如何在 Go 中解析带有动态键的 YAML 文件?

Barbara Streisand
Barbara Streisand原创
2024-11-04 02:38:29422浏览

How to Parse YAML Files with Dynamic Keys in Go?

YAML 解析带有动态键的 Go 代码

要解析 YAML 文件中包含动态键的 Go 代码,请遵循以下步骤:

1. 安装 yaml 库:

<code class="sh">go get gopkg.in/yaml.v2</code>

2. 定义模型结构:
创建结构体以表示 YAML 数据。如果您知道所有可能的键,可以使用常规字段定义它们,否则可以使用 map[string]interface{} 字段包含动态键。

3. 使用自定义 Unmarshaler(可选):
如果您需要自定义如何解析 YAML 数据,则可以使用自定义 Unmarshaler。这可以帮助您处理动态键或其他复杂解析场景。

4. 解析 YAML 数据:
使用 yaml.Unmarshal 函数解析 YAML 数据并将其反序列化为模型结构。

示例:
以下示例演示如何解析具有动态键的 YAML 文件:

<code class="go">package main

import (
    "fmt"

    "gopkg.in/yaml.v2"
)

var data = `
---
development:
  skip-header-validation: true
  V1:
    current: "1.0.0"
    mime_types:
      - application/vnd.company.jk.identity+json;
      - application/vnd.company.jk.user+json;
      - application/vnd.company.jk.role+json;
      - application/vnd.company.jk.scope+json;
      - application/vnd.company.jk.test+json;
    skip-mime-type-validation: true
    skip-version-validation: true
  V2:
    current: "2.0.0"
    mime_types:
      - application/vnd.company.jk.identity+json;
      - application/vnd.company.jk.user+json;
      - application/vnd.company.jk.role+json;
      - application/vnd.company.jk.scope+json;
      - application/vnd.company.jk.test+json;

`

type MajorVersion struct {
    Current                 string   `yaml:"current"`
    MimeTypes               []string `yaml:"mime_types"`
    SkipVersionValidation  bool     `yaml:"skip-version-validation"`
    SkipMimeTypeValidation bool     `yaml:"skip-mime-type-validation"`
}

type Environment struct {
    SkipHeaderValidation bool
    Versions             map[string]MajorVersion `yaml:",inline"`
}

func main() {
    e := map[string]Environment{}
    if err := yaml.Unmarshal([]byte(data), &e); err != nil {
        fmt.Println(err.Error())
    }
    fmt.Printf("%#v\n", e)
}</code>

输出:

map[string]main.Environment{
    "development": {
        SkipHeaderValidation: true,
        Versions: {
            "V1": {
                Current:                "1.0.0",
                MimeTypes:              {"application/vnd.company.jk.identity+json;", "application/vnd.company.jk.user+json;", "application/vnd.company.jk.role+json;", "application/vnd.company.jk.scope+json;", "application/vnd.company.jk.test+json;"},
                SkipVersionValidation:  true,
                SkipMimeTypeValidation: true,
            },
            "V2": {
                Current:                "2.0.0",
                MimeTypes:              {"application/vnd.company.jk.identity+json;", "application/vnd.company.jk.user+json;", "application/vnd.company.jk.role+json;", "application/vnd.company.jk.scope+json;", "application/vnd.company.jk.test+json;"},
                SkipVersionValidation:  false,
                SkipMimeTypeValidation: false,
            },
        },
    },
}

以上是如何在 Go 中解析带有动态键的 YAML 文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn