YAML for Configuration File In Go Language
Go language is programming language from Google which is made in 2009 for server programming. Go become popular in this year since it use very simple code based on C language. It has many feature and flexibility to write our software algorithms. When we build software or application, usually we must define configuration inside the package. The config file usually consists of workflows, environment variables, service definition, and other information to run the programs.
In Go, most programmer write their application configuration in format of JSON because it’s simplicity and Go has built-in library to interact with JSON. But, sometimes JSON become very complex and hard for user to understand if we write a workflow in format of JSON because there will has many bracket-in-bracket. So, I want to share my experience using YAML for configuration file to handle some issues in JSON. Here are some comparison between JSON and YAML.
YAML
#this is example of comments in YAML structureparent:
child:
child:
child:
data1: string_value
data2: 10
data3:
child: true
- add comments is possible denoted with #
- string quotes are optional but it supports using single or double quotes
- root node can be any valid data types
- use double space characters to define object level
- bigger size than JSON
- assign value with reference is possible
JSON
{
"parent":{
"child":{
"child":{
"child":{
"data1": "string_value",
"data2": 10,
"data3":{
"child": true
}
}
}
}
}
}
- can be read faster than other types
- string value must be in double quotes
- root node must be either array or object
- comments are not possible
- smaller size than YAML
- cannot assign value to refer other value
Before interact with YAML file in Go, we must import a library from “gopkg.in/yaml.v2”
. Just like marshal and unmarshal in JSON, when we want to marshal YAML to struct we use tag called yaml
followed by the name of an object. Here are example of code for mashall YAML in GO.
import (
"io/ioutil"
"log"
`3"gopkg.in/yaml.v2"
)type YamlObject struct{
Name string `yaml:"name"`
Date string `yaml:"date"`
Description string `yaml:"description"`
}func readYaml(path string){
yamlFile, err := ioutil.ReadFile(path) if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
} err = yaml.Unmarshal(yamlFile, response)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
}
This YAML file is very useful when we have to declare object-in-object with large number of data. It can be read and understand easily. We can put a comment in it to add some information or detail. But we must careful with spacing since it is very crucial in defining level of object.
Conclusion
Using JSON or YAML as our configuration file has their own pro and cons. It depends on our needs, do we need a clear and readable config or we only concern about reading speed. Practices makes better, so we have to try any new coming technology or methodology then we know which one is suitable for our project. I hope this article can help you to know better about YAML in Go. I really open with comments, suggestion, and diccussion. Thank you.