go - Reading a YAML file in Golang -
i have issue reading yaml file. think it's in file structure can't figure out what.
yaml file:
conf: hits:5 time:5000000
code:
type conf struct { hits int64 `yaml:"hits"` time int64 `yaml:"time"` } func (c *conf) getconf() *conf { yamlfile, err := ioutil.readfile("conf.yaml") if err != nil { log.printf("yamlfile.get err #%v ", err) } err = yaml.unmarshal(yamlfile, c) if err != nil { log.fatalf("unmarshal: %v", err) } return c }
your yaml file must
hits: 5 time: 5000000
your code should this:
package main import ( "fmt" "gopkg.in/yaml.v2" "io/ioutil" "log" ) type conf struct { hits int64 `yaml:"hits"` time int64 `yaml:"time"` } func (c *conf) getconf() *conf { yamlfile, err := ioutil.readfile("conf.yaml") if err != nil { log.printf("yamlfile.get err #%v ", err) } err = yaml.unmarshal(yamlfile, c) if err != nil { log.fatalf("unmarshal: %v", err) } return c } func main() { var c conf c.getconf() fmt.println(c) }
the main error capital letter struct.
Comments
Post a Comment