gpt4 book ai didi

戈朗 : Retrieve Nested keys in YAML

转载 作者:IT王子 更新时间:2023-10-29 01:09:35 25 4
gpt4 key购买 nike

这是我的 YAML 文件:

hosts: all
gather_facts: no
remote_user: ubuntu
name: install latest nginx
tasks:
- name: install the nginx key
apt_key:
url: http://nginx.org/keys/nginx_signing.key
state: present
become: yes

- name: install aws cli
command: pip3 install awscli
become: yes

这是我的 代码:

package main

import (
"github.com/davecgh/go-spew/spew"
"gopkg.in/yaml.v2"
"io/ioutil"
)

type Config struct {
Hosts string `yaml:hosts`
Gather_facts string `yaml:gatherfacts`
Remote_user string `yaml:remoteuser`
Name string `yaml:names`
Tasks []map[string]string `yaml:tasks`
}

func main() {
file, err := ioutil.ReadFile("/path-to-nginx1.yml")
if err != nil {
panic(err)
}
var config Config
yaml.Unmarshal(file, &config)
spew.Dump(config)
}

这是输出:

(main.Config) {
Hosts: (string) (len=3) "all",
Gather_facts: (string) (len=2) "no",
Remote_user: (string) (len=6) "ubuntu",
Name: (string) (len=20) "install latest nginx",
Tasks: ([]map[string]string) (len=2 cap=2) {
(map[string]string) (len=2) {
(string) (len=6) "become": (string) (len=3) "yes",
(string) (len=4) "name": (string) (len=21) "install the nginx key"
},
(map[string]string) (len=3) {
(string) (len=4) "name": (string) (len=15) "install aws cli",
(string) (len=7) "command": (string) (len=19) "pip3 install awscli",
(string) (len=6) "become": (string) (len=3) "yes"
}
}
}

问题:如何从我的 YAML 中检索以下 key ?

apt_key:
url: http://nginx.org/keys/nginx_signing.key
state: present

目前,我的 Go 解析器完全忽略了输出中的上述键。

此外,我有许多 YAML 文件,它们都有不同程度的嵌套。其中大部分在文件本身内有不同程度的嵌套。那么我的 struct 是否需要修改以解决每个单独的 key ?或者,什么是更好的 处理每个键具有不同嵌套级别的 YAML 文件的方法?

>>>更新<<<:

我通过修改 struct 中的 Tasks 取得了一些进展,如下所示:

type Config struct {
Hosts string `yaml:hosts`
Gather_facts string `yaml:gatherfacts`
Remote_user string `yaml:remoteuser`
Name string `yaml:names`
Tasks []struct {
Name string `yaml:name`
Apt_key struct {
Url string `yaml:url`
State string `yaml:url`
} `yaml:apt_key`
Become string `yaml:become`
}
}

输出:

(main.Config) {
Hosts: (string) (len=3) "all",
Gather_facts: (string) (len=2) "no",
Remote_user: (string) (len=6) "ubuntu",
Name: (string) (len=20) "install latest nginx",
Tasks: ([]struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) (len=2 cap=2) {
(struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) {
Name: (string) (len=21) "install the nginx key",
Apt_key: (struct { Url string "yaml:url"; State string "yaml:url" }) {
Url: (string) (len=39) "http://nginx.org/keys/nginx_signing.key",
State: (string) (len=7) "present"
},
Become: (string) (len=3) "yes"
},
(struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) {
Name: (string) (len=15) "install aws cli",
Apt_key: (struct { Url string "yaml:url"; State string "yaml:url" }) {
Url: (string) "",
State: (string) ""
},
Become: (string) (len=3) "yes"
}
}
}

所以现在我可以看到之前完全缺失的 apt_key 部分。但是,我没有找到用 YAML 编写的 command 部分:

      command: pip3 install awscli

我如何获得它?

此外,我感觉不太好,因为我必须在 struct 中声明几乎每个键,这在这种情况下没问题,因为我的 YAML 是几乎没有 15 行长。但如果 YAML 更大更长,这将是丑陋和麻烦的。我相信在 中一定有更好更有效的方法处理 YAML 文件。

最佳答案

它不起作用,因为您正在尝试解码两个不同的集合:name、apt-key、become 和 name、command、become 使用相同的结构。这是不一致的。

关于戈朗 : Retrieve Nested keys in YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com