Reputation: 31
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 ?
Upvotes: 1
Views: 714
Reputation: 31
OK, it was quite stupid...
But it can help others.
Putting the values inside of " " solved the problem. eg.
name: "siteName1"
url: "http://siteUrl1"
Upvotes: 2