GoYAML 结构切片

1 yaml go slice

我正在尝试学习 goyaml,并且在尝试从 yaml 生成切片时遇到一些问题。我可以将我的结构编组到此示例 yaml 中,但无法将相同的 yaml 解组回结构中。

 input:
  file:
  - file: stdin
  webservice:
  - port: "0000"
    address: 0.0.0.0
processing:
  regex:
  - regex: ^(this)*
    mapping: (1) thing
output:
  file:
  - file: stdout
  webservice:
  - port: "0000"
    address: 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

结构:

type Configuration struct{
    Input ioInstance
    Processing ProcessingInstance
    Output ioInstance
}

type ioInstance struct {
    File []FileInstance
    Webservice []WebserviceInstance
}

type FileInstance struct {
    File string
}

type WebserviceInstance struct  {
    Port string
    Address string
}

type ProcessingInstance struct {
    Regex []RegexInstance
}

type RegexInstance struct {
    Regex string
    Mapping string
}
Run Code Online (Sandbox Code Playgroud)

在测试解组时,我遇到了反映错误,据我了解,我认为我可能需要在没有结构切片的情况下重新进行设计,我希望有人可以对此提供一些见解?

错误:

panic: reflect: reflect.Value.Set using unaddressable value [recovered]
    panic: reflect: reflect.Value.Set using unaddressable value [recovered]
    panic: reflect: reflect.Value.Set using unaddressable value
Run Code Online (Sandbox Code Playgroud)

Lem*_*eId 5

以下代码对我来说效果很好,按应有的方式输出原始 yaml 文本:

package main

import "fmt"
import "gopkg.in/yaml.v2"

func main() {
    c := &Configuration{}
    yaml.Unmarshal([]byte(yamlText), c) 
    buf, _ := yaml.Marshal(c)
    fmt.Println(string(buf))
}

var yamlText = ` 
input:
  file:
  - file: stdin
  webservice:
  - port: "0000"
    address: 0.0.0.0
processing:
  regex:
  - regex: ^(this)*
    mapping: (1) thing
output:
  file:
  - file: stdout
  webservice:
  - port: "0000"
    address: 0.0.0.0
`

type Configuration struct {
    Input      ioInstance
    Processing ProcessingInstance
    Output     ioInstance
}

type ioInstance struct {
    File       []FileInstance
    Webservice []WebserviceInstance
}

type FileInstance struct {
    File string
}

type WebserviceInstance struct {
    Port    string
    Address string
}

type ProcessingInstance struct {
    Regex []RegexInstance
}

type RegexInstance struct {
    Regex   string
    Mapping string
}
Run Code Online (Sandbox Code Playgroud)

输出:

input:
  file:
  - file: stdin
  webservice:
  - port: "0000"
    address: 0.0.0.0
processing:
  regex:
  - regex: ^(this)*
    mapping: (1) thing
output:
  file:
  - file: stdout
  webservice:
  - port: "0000"
    address: 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

(当然,您应该忽略实际代码中的错误,就像我在这里所做的那样。)

编辑:

Unmarshal需要一个指向结构的指针才能设置其字段。如果您使用普通结构体值调用它,它只会接收原始结构体的副本(因为在 Go 中,所有内容都作为副本传递),因此不可能更改原始结构体的字段。

因此,您基本上有两种选择:

您可以c使用 来定义和初始化c := &Configuration{},即将其定义为类型指针Configuration,同时将其指向一个新的归零Configuration值。然后就可以打电话了yaml.Unmarshal([]byte(yamlText), c)

或者,您可以定义cwith var c Configuration,这意味着c不是指针,而是类型 的新零值ConfigurationUnmarshal在这种情况下,您需要在调用:时显式传递指向该值的指针yaml.Unmarshal([]byte(yamlText), &c)

请注意,传递给 Unmarshal 的指针必须指向现有 Configuration值。var c *Configuration会定义c为一个指针,但立即将其传递给Unmarshal会导致恐慌,因为它的值是nil; 它并不指向现有的Configuration值。

另外,在我上面的代码中最初有一个小拼写错误,现在已修复(尽管代码仍然有效)。我写了c := &Configuration{}yaml.Unmarshal([]byte(yamlText), &c),所以我实际上传递了Unmarshal 一个指向 struct 的指针,它Unmarshal能够毫无问题地处理。