在Go语言中解析YAML:列表中的映射

huangapple go评论96阅读模式
英文:

Parsing YAML in Go : map in list

问题

我正在尝试解析一个yaml文件,用于我的一个小项目。

目标是将应用程序的信息存储在配置文件中,包括服务器文件的地址,以防需要更新。这样做是为了方便进行编辑。

主要问题是在应用程序真正启动之前需要进行一些连接测试。我正在尝试解析该文件。
文件的内容如下:

conf.yaml

app:
    version:    "1"
    name:       MySuperApp
    configLocation:   http://configaddress

test_url:
  -
    name:       siteName1
    url:        http://siteUrl1
  -
    name:       siteName2
    url:        http://siteUrl2
    proxy_port: 5678

我编写了以下代码,可以获取app部分的内容,但无法获取test_url部分的内容:

package main

import (
    "fmt"
    "io/ioutil"
	"path/filepath"
    "gopkg.in/yaml.v2"
)

type AppInfo struct {
    Name 	string 	`yaml:"name"`
	Version string 	`yaml:"version"`
}

type Config struct {
	App AppInfo	`yaml:"app"`
}

type TestUrl struct {
	Name 	    string `yaml:"name"`
    Url 	    string `yaml:"url"`
	ProxyPort 	string `yaml:"proxy_port,omitempty"`
}

type TestUrls struct {
	ATest []TestUrl `yaml:"test_url"`
}

func main() {
    filename, _ 	:= filepath.Abs("./config/conf.yaml")
	yamlFile, err 	:= ioutil.ReadFile(filename)

    if err != nil {
	    panic(err)
	}

	var config Config
    err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
    	panic(err)
	}

	var test TestUrls
    err = yaml.Unmarshal(yamlFile, &test)
	if err != nil {
    	panic(err)
	}

    fmt.Println("Application : ", config.App.Name,"\nVersion : ", config.App.Version)

	fmt.Println(test)
}

输出结果为:

Application : MySuperApp
Version : 1
{[]}

我漏掉了什么?

英文:

I'm trying to parse a yaml file for a small project I have.

The goal is to have the app's infos in a config file including the address to the serverfile in case it need to be updated. It is in a config file for easy "editability" purposes.

The main thing is that there are some connectivity tests to be done before the app really starts. I'm trying to parse that file.
It looks like this :

conf.yaml

app:
    version:    "1"
    name:       MySuperApp
    configLocation:   http://configaddress

test_url:
  -
    name:       siteName1
    url:        http://siteUrl1
  -
    name:       siteName2
    url:        http://siteUrl2
    proxy_port: 5678

I wrote the following, I can get what's in app: but not what's in test_url :

package main

import (
    "fmt"
    "io/ioutil"
	"path/filepath"
    "gopkg.in/yaml.v2"
)

type AppInfo struct {
    Name 	string 	`yaml:"name"`
	Version string 	`yaml:"version"`
}

type Config struct {
	App AppInfo	`yaml:"app"`
}

type TestUrl struct {
	Name 	    string `yaml:"name"`
    Url 	    string `yaml:"url"`
	ProxyPort 	string `yaml:"proxy_port,omitempty"`
}

type TestUrls struct {
	ATest []TestUrl `yaml:"test_url"`
}

func main() {
    filename, _ 	:= filepath.Abs("./config/conf.yaml")
	yamlFile, err 	:= ioutil.ReadFile(filename)

    if err != nil {
	    panic(err)
	}

	var config Config
    err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
    	panic(err)
	}

	var test TestUrls
    err = yaml.Unmarshal(yamlFile, &test)
	if err != nil {
    	panic(err)
	}

    fmt.Println("Application : ", config.App.Name,"\nVersion : ", config.App.Version)

	fmt.Println(test)
}

As an output I get :

Application : MySuperApp
Version : 1
{[]}

What am I missing ?

答案1

得分: 2

好的,以下是翻译好的内容:

好的,这真的很愚蠢...

但它可以帮助其他人。

将值放在" "中解决了问题。
例如:

name:       "siteName1"
url:        "http://siteUrl1"
英文:

OK, it was quite stupid...

But it can help others.

Putting the values inside of " " solved the problem.
eg.

name:       "siteName1"
url:        "http://siteUrl1"

huangapple
  • 本文由 发表于 2016年11月24日 17:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/40783154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定