Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a YAML file

Tags:

yaml

go

I have an issue with reading a YAML file. I think it's something in the file structure but I 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 } 
like image 673
MIkCode Avatar asked Jun 19 '15 21:06

MIkCode


People also ask

Is YAML easy to read?

YAML is an easy, expressive, data-oriented language that distinguishes itself from document markup languages. YAML Ain't a Markup Language (YAML), and as configuration formats go, it's easy on the eyes.

Is YAML human-readable?

YAML (/ˈjæməl/ and YAH-ml) (see § History and name) is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.

Is YAML machine readable?

YAML is a data serialization language that, like JSON, allows you to store information in a human-readable format.

What is a YAML file in Python?

YAML (YAML Ain't Markup Language) is a human-readable data-serialization language. It is commonly used for configuration files, but it is also used in data storage (e.g. debugging output) or transmission (e.g. document headers).


2 Answers

your yaml file must be

hits: 5 time: 5000000 

your code should look like 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 was capital letter for your struct.

like image 50
qwertmax Avatar answered Oct 12 '22 23:10

qwertmax


Using an upgraded version 3 of yaml package.

An example conf.yaml file:

conf:   hits: 5   time: 5000000   camelCase: sometext 

The main.go file:

package main  import (     "fmt"     "io/ioutil"     "log"      "gopkg.in/yaml.v3" )  type myData struct {     Conf struct {         Hits      int64         Time      int64         CamelCase string `yaml:"camelCase"`     } }  func readConf(filename string) (*myData, error) {     buf, err := ioutil.ReadFile(filename)     if err != nil {         return nil, err     }      c := &myData{}     err = yaml.Unmarshal(buf, c)     if err != nil {         return nil, fmt.Errorf("in file %q: %v", filename, err)     }      return c, nil }  func main() {     c, err := readConf("conf.yaml")     if err != nil {         log.Fatal(err)     }     fmt.Printf("%v", c) } 

Running instructions (in case it's the first time you step out of stdlib):

go mod init example.com/whatever go get gopkg.in/yaml.v3 cat go.sum go run . 

The tags (like yaml:"field") are optional for any all-lowercase yaml key identifiers. For shows I've included one for a camelCase identifier.

(Confusingly, this lowercasing behavior of "yaml" is not seen in the standard "json" package. If your structure is used with both JSON and YAML encodings it is best to specify both tags on literally every field. Verbose, but reduces mistakes.)

like image 31
kubanczyk Avatar answered Oct 13 '22 00:10

kubanczyk