首頁  >  文章  >  後端開發  >  如何在 Go 中解析具有動態鍵的 YAML 檔案?

如何在 Go 中解析具有動態鍵的 YAML 檔案?

Barbara Streisand
Barbara Streisand原創
2024-11-04 02:38:29476瀏覽

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