Go 解析yaml返回空对象
我有以下yaml需要解析为struct。 在builds属性中,我在调试时得到了空值,我在这里缺少什么 我使用“gopkg.in/yaml.v2” 结构是Go 解析yaml返回空对象,go,yaml,Go,Yaml,我有以下yaml需要解析为struct。 在builds属性中,我在调试时得到了空值,我在这里缺少什么 我使用“gopkg.in/yaml.v2” 结构是 type Runs struct { Name string Type string Path string `yaml:"path,omitempty"` Builds []Builds `yaml:”builds,omitempty"` } type Bui
type Runs struct {
Name string
Type string
Path string `yaml:"path,omitempty"`
Builds []Builds `yaml:”builds,omitempty"`
}
type Builds struct {
Name string `yaml:"name,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
}
type Properties map[string]string
确保yaml文件的格式正确。看看这个 下面的代码运行良好
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
)
type Runs struct {
Name string
Type string
Path string `yaml:"path,omitempty"`
Builds []Builds `yaml:”builds,omitempty"`
}
type Builds struct {
Name string `yaml:"name,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
}
type Properties map[string]string
func main() {
data := `builds:
-
name: db
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml: {\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa
name: srv
path: srv
type: java
`
runs := Runs{}
err := yaml.Unmarshal([]byte(data), &runs)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t:\n%v\n\n", runs)
d, err := yaml.Marshal(&runs)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t dump:\n%s\n\n", string(d))
}
我希望这有帮助正确的YAML是首先要考虑的问题。 如果你想运行一个
,
,你应该让你的yaml格式化成这样
name: srv
builds:
-
name: db
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
{\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa
path: srv
type: java
但是我想得到更多的这个对象,你需要把它们组合在一个参数中。它可以像这样
runs:
-
name: srv
builds:
-
name: db
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
{\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa
path: srv
type: java
-
name: srv2
builds:
-
name: db2
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
{\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa2
path: srv2
type: java2
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)
type Document struct {
Runs []Runs `yaml:"runs,omitempty"`
}
type Runs struct {
Name string `yaml:"name,omitempty"`
Type string `yaml:"type,omitempty"`
Path string `yaml:"path,omitempty"`
Builds []Builds `yaml:"builds,omitempty"`
}
type Builds struct {
Name string `yaml:"name,omitempty"`
Properties map[string]string `yaml:"properties,omitempty"`
}
func main() {
var document Document
reader, err := os.Open("demo.yml")
if err != nil {
log.Fatal(err)
}
buf, _ := ioutil.ReadAll(reader)
yaml.Unmarshal(buf, &document)
if err := yaml.Unmarshal(buf, &document); err != nil {
fmt.Print(err)
os.Exit(1)
}
fmt.Println(document)
}
然后在你的代码中可能是这样的
runs:
-
name: srv
builds:
-
name: db
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
{\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa
path: srv
type: java
-
name: srv2
builds:
-
name: db2
properties:
JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
{\"service_name\" : \"~{h-container}\"}]"
TEST2: aaaa2
path: srv2
type: java2
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)
type Document struct {
Runs []Runs `yaml:"runs,omitempty"`
}
type Runs struct {
Name string `yaml:"name,omitempty"`
Type string `yaml:"type,omitempty"`
Path string `yaml:"path,omitempty"`
Builds []Builds `yaml:"builds,omitempty"`
}
type Builds struct {
Name string `yaml:"name,omitempty"`
Properties map[string]string `yaml:"properties,omitempty"`
}
func main() {
var document Document
reader, err := os.Open("demo.yml")
if err != nil {
log.Fatal(err)
}
buf, _ := ioutil.ReadAll(reader)
yaml.Unmarshal(buf, &document)
if err := yaml.Unmarshal(buf, &document); err != nil {
fmt.Print(err)
os.Exit(1)
}
fmt.Println(document)
}
请分享您尝试过的内容…谢谢1+,我的yaml是有效的,可能问题的副本更改了它,但我能够解析它,否则我猜如果解组失败,我会出错…知道它可能是什么吗?谢谢1+,我的yaml是有效的,可能问题的副本更改了它,但我能够解析它,否则,我想我会得到和错误,如果解组将失败…你知道它可能是什么吗?是否更改了结构中的某些内容?如果未明确检查
yaml.Unmarshal
是否返回错误,则代码将以空运行。我在我的代码中添加了这个,如果检查,确定你可以发送给我,我可以通过聊天或其他方式发送吗?让我们。