登录
首页 >  Golang >  Go问答

将 Go 语言应用于 YAML 文件

来源:stackoverflow

时间:2024-03-11 20:30:27 371浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《将 Go 语言应用于 YAML 文件》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我编写了一个 golang 程序,它将规则附加到文件中,如下所述 所需格式:

customrules:
  custom-rules.yaml: |-
    - rule: pod created in kube namespace
      append: true
      condition: and (k8s_audit_never_true)
      source: k8s_audit
    - rule: create files below dev
      append: true
      condition: and (never_true)
      source: syscall

我编写了一个 go 程序,但未能采用上述格式,我无法得到我缺少的内容。

package main

import (
    "fmt"
    "io/ioutil"
    "log"

    "gopkg.in/yaml.v2"
)

type autogenerated struct {
    customrules customrules `yaml:"customrules"`
}
type customrulesyaml struct {
    rule      string `yaml:"rule"`
    append    bool   `yaml:"append"`
    condition string `yaml:"condition"`
    source    string `yaml:"source"`
}
type customrules struct {
    customrulesyaml []customrulesyaml `yaml:"custom-rules.yaml"`
}

func main() {
    // yfile, err := ioutil.readfile("/home/revaa/falco/custom_rules.yaml")
    // if err != nil {
    //  log.fatal(err)
    // }
    c1 := customrulesyaml{"k8s serviceaccount created", false, "(never_true)", "k8s-audit"}
    c2 := customrulesyaml{"k8s service created", false, "never_true", "k8s-audit"}
    c := []customrulesyaml{c1, c2}
    c3 := customrules{c}
    data := autogenerated{c3}
    check, err := yaml.marshal(&data)

    if err != nil {

        log.fatal(err)
    }

    err2 := ioutil.writefile("/home/revaa/falco/custom_rules.yaml", check, 0)

    if err2 != nil {

        log.fatal(err2)
    }

    fmt.println("data written")

}

这是我的 go 代码,在运行程序时,yaml 不会以上述格式附加。这些值按如下方式附加。

customRules:
  custom-rules.yaml:
  - rule: K8s serviceaccount created
    append: false
    condition: (never_true)
    source: k8s-audit
  - rule: k8s service created
    append: false
    condition: never_true
    source: k8s-audit

为什么我没有获得所需格式的 yaml 文件?


正确答案


您所需的输入格式代表以下 yaml 结构:

  • 有一本包含一个键值对的字典。
    • 密钥是 customrules
    • 该值是一个字典。

存储在上述值中的字典有一个条目:

  • 密钥是 custom-rules.yaml
  • 该值是一个字符串。

上面的值中存储的字符串是:

"- rule: pod created in kube namespace\n  append: true\n  condition: and (k8s_audit_never_true)\n  source: k8s_audit\n- rule: create files below dev\n  append: true\n  condition: and (never_true)\n  source: syscall"

也就是说,这个不是列表类型。这是一个单一的字符串。

现在,事实是这个单个字符串有效的——尽管有点狡猾——yaml,并且如果读取,它将产生一个列表(go中的一个切片) )的2个元素,每个元素都是一个字典(一般对应go中的map或struct类型)。

如果您确实需要这个,那么您的代码接近正确。您需要封送两次,而不是将 []customrulesyaml 包装在类型中。 Here's a variant of your code on the Go Playground,其输出为:

custom-rules.yaml: |
  - rule: k8s serviceaccount created
    append: false
    condition: (never_true)
    source: k8s-audit
  - rule: k8s service created
    append: false
    condition: never_true
    source: k8s-audit

现在,请注意此输出有一个管道符号 |,没有后缀连字符 -。这是因为编组字符串 string(asbytes) 以换行符结尾。它可能应该以换行符结尾,这就是 yaml.marshal 生成换行符的原因。但是您的示例输入以换行符结尾,这就是为什么您的示例输入有 |- 而不仅仅是 |- 意味着“不包含该换行符” .

要从现有代码中获取,您必须去掉换行符,例如添加:

asBytes = asBytes[0:len(asBytes)-1] // delete trailing newline

c3 中构建字符串并对其进行封送之前。

到这里,我们也就讲完了《将 Go 语言应用于 YAML 文件》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>