Wednesday, March 20, 2024

Golang Tutorial: How to Read YAML File in Go


Managing application configurations efficiently becomes imperative. Configuration files store crucial settings and parameters that determine an application's behavior, allowing developers to tweak settings without altering the source code. Among various formats for configuration files, YAML (YAML Ain’t Markup Language) has emerged as a popular choice due to its human-readable syntax and widespread adoption in modern frameworks and tools like AWS CloudFormation, Kubernetes, and Swagger.

YAML is a serialization language designed for human readability, making it intuitive for both developers and non-developers. Its syntax is strict, prohibiting the use of tabs and enforcing spacing between elements. Additionally, YAML is case-sensitive and commonly uses file extensions ".yaml" or ".yml". Notably, YAML serves as a superset of JSON, offering enhanced readability and flexibility.

Go Struct Tags

In Golang, structs play a pivotal role in organizing data. They group related fields together to form a cohesive unit, resembling lightweight classes without inheritance. Struct tags, on the other hand, provide metadata information embedded within struct fields. These tags, leveraged through reflection, offer instructions on encoding and decoding struct fields into various formats, facilitating seamless data manipulation.


Implementation

Let's delve into implementing YAML parsing in Golang. Suppose we have an application configuration file named "app_config.yml" structured as follows:

server:

  port: 8080


dbConfig:

  host: db-host

  username: admin

  password: admin_pwd


security:

  sslEnabled: true

  truststoreLocation: ./keystore

  truststorePassword: changeit

To parse this YAML file, we define Go structs corresponding to each configuration section:

// server_config.go

package domain


type ServerRoot struct {

Server ServerConfig `yaml:"server"`

}


type ServerConfig struct {

Port string `yaml:"port"`

}


// db_config.go

package domain


type DbConfigRoot struct {

DbConfig DbConfig `yaml:"dbConfig"`

}


type DbConfig struct {

Host     string `yaml:"host"`

UserName string `yaml:"username"`

Password string `yaml:"password"`

}


// security_config.go

package domain


type SecurityConfigRoot struct {

SecurityConfig SecurityConfig `yaml:"security"`

}


type SecurityConfig struct {

SslEnabled         bool   `yaml:"sslEnabled"`

TruststoreFilePath string `yaml:"truststoreLocation"`

TruststorePwd      string `yaml:"truststorePassword"`

}

We then read the YAML file and parse its content into corresponding Go types using the YAML API:

yamlData, err := os.ReadFile("app_config.yml")


func loadServerConfig(yamlData []byte) {

yaml.Unmarshal(yamlData, &ServerConfig)

fmt.Println("Loaded Server Config")

}


func loadDBConfig(yamlData []byte) {

yaml.Unmarshal(yamlData, &DbConfig)

fmt.Println("Loaded DB Config")

}


func loadSecurityConfig(yamlData []byte) {

yaml.Unmarshal(yamlData, &SecurityConfig)

fmt.Println("Loaded Security Config")

}

Conclusion

In this tutorial, we've explored the synergy between YAML and Golang for efficient application configuration management. By leveraging YAML's human-readable syntax and Golang's robust struct tags, developers can seamlessly parse and manipulate configuration files. This approach not only enhances maintainability but also fosters a scalable architecture conducive to modern microservices environments.

0 comments:

Post a Comment