SoFunction
Updated on 2025-03-06

Summary of the method of Golang parsing yaml files

The yaml file is the most commonly used configuration file at present. When writing code and tools in the Go language, the yaml file will also be used, and the service configuration and middleware information will be defined into the yaml file. It can be selected according to the actual scenario in the future.

//Download the external package firstgo get -u /yaml.v2

1. Define the yaml configuration file

conf/

ck:
  host: 11.11.11.11
  port: 9001
  database: db2
  username: test2
  password: 123ck456
user: test
password: 123yy456
host: 10.10.10.10
port: 3306
dbname: db1

2. Main program

package main

import (
    "database/sql"
    "fmt"
    "/yaml.v3"
    "io/ioutil"
    "time"
)

type Conn struct {
    Ck struct {
        Host     string `yaml:"host"`
        Port     string `yaml:"port"`
        Database string `yaml:"database"`
        Username string `yaml:"username"`
        Password string `yaml:"password"`
    }
}
type Mysql struct {
    User     string `yaml:"user"`
    Password string `yaml:"password"`
    Host     string `yaml:"host"`
    Port     string `yaml:"port"`
    DBname   string `yaml:"dbname"`
}

const cfgFile = "conf/"

func main() {
    data, err := (cfgFile)//Read the path to the configuration file    if err != nil {
        ("err: %v\n", err)
        return
    }

    var conf Conn
    if err := (data, &conf); err != nil {//Deserialize the information in the yaml file to the conf structure        ("err: %v\n", err)
        return
    }
    yml, _ := (conf)//Serialize to yaml file    ("conf: %#v\n", conf)
    ("%s\n", yml)

    var confs Mysql
    if err := (data, &confs); err != nil {
        ("err: %v\n", err)
        return
    }
    yaml, _ := (confs)
    ("conf: %#v\n", confs)
    ("%s\n", yaml)

    //Connect the database    user := 
    password := 
    host := 
    port := 
    database := 
    dataSourceName := user + ":" + password + "@tcp(" + host + ":" + port + ")/" + database + "?charset=utf8&parseTime=True"
    conn, err := ("mysql", dataSourceName)
    if err != nil {
        return
    }
    ( * 30)     //The maximum connection timeout time should be less than the link timeout time of the database itself    (10)                  //Maximum number of idle connections (the connection that can be obtained at the same time during concurrency is also the interoperable connection that is put back into the pool after use, thereby improving performance)    (100)                 //The maximum number of open connections, 0 is not limited
    //Remember to close the connection after ending    defer ()
}

3. Output result

conf: {Ck:struct { Host string "yaml:\"host\""; Port string "yaml:\"por
t\""; Database string "yaml:\"database\""; Username string "yaml:\"username\"";
Password string "yaml:\"password\"" }{Host:"11.11.11.11", Port:"9001", Database:
"db2", Username:"test2", Password:"123ck456"}}
ck:
    host: 11.11.11.11
    port: "9001"
    database: db2
    username: test2
    password: 123ck456

conf: {User:"test", Password:"123yy456", Host:"10.10.10.10", Port:"330
6", DBname:"db1"}
user: test
password: 123yy456
host: 10.10.10.10
port: "3306"
dbname: db1

4. Supplement

4.1 Deprecated 'ReadFile'

Starting in Go 1.16, , and , are deprecated because the io/ioutil package is deprecated.

The solution is as follows: Use the same method in the io or os package to replace it, that is, modify yourself by referring to the following to modify the package name

 -> 
     -> 
     -> 
    // others
     -> 
     -> 
     -> 
     -> 
     -> 

4.2 Variable naming rules in yaml file

Variables in yaml files must be in lowercase letters. Use capital letters, midscores, underscores, etc., such as CK: 127.1.1.1 mySQL: xxx Myql: xxx my_sql: xxx will have problems when interpreting the yaml configuration file, and it will be interpreted as empty value and will not report an error.

This is the article about the summary of Go yaml file method. For more related Go yaml content, please search for my previous article or continue browsing the following related articles. I hope you can support me in the future.