How Go- read yaml, json, ini configuration files, etc.

1. json use

JSON should be more familiar, it is a lightweight data interchange format. Hierarchy simple and clear, easy to read and write, but also easy for machines to parse and generate.

  1. Creating conf.json:
{
  "enabled": true,
  "path": "/usr/local"
}
  1. New config_json.go:
package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type configuration struct {
    Enabled bool
    Path    string
}

func main() {
    // 打开文件
    file, _ := os.Open("conf.json")

    // 关闭文件
    defer file.Close()

    //NewDecoder创建一个从file读取并解码json对象的*Decoder,解码器有自己的缓冲,并可能超前读取部分json数据。
    decoder := json.NewDecoder(file)

    conf := configuration{}
    //Decode从输入流读取下一个json编码值并保存在v指向的值里
    err := decoder.Decode(&conf)
    if err != nil {
        fmt.Println("Error:", err)
    }
    fmt.Println("path:" + conf.Path)
}

After starting the operation, the output is as follows:

D:\Go_Path\go\src\configmgr>go run config_json.go
path:/usr/local

2. ini use

INI file format is an informal platform or certain standard configuration file on the software, consists of sections (section) and key (key), more commonly used in Microsoft Windows operating systems. This configuration file file extension INI.

  1. Creating conf.ini:
[Section]
enabled = true
path = /usr/local # another comment

2. Download third-party libraries: go get gopkg.in/gcfg.v1

  1. New config_ini.go:
package main

import (
    "fmt"
    gcfg "gopkg.in/gcfg.v1"
)

func main() {
    config := struct {
        Section struct {
            Enabled bool
            Path    string
        }
    }{}

    err := gcfg.ReadFileInto(&config, "conf.ini")

    if err != nil {
        fmt.Println("Failed to parse config file: %s", err)
    }
    fmt.Println(config.Section.Enabled)
    fmt.Println(config.Section.Path)
}

After starting the operation, the output is as follows:

D:\Go_Path\go\src\configmgr>go run config_ini.go
true
/usr/local

3. yaml use

yaml that may be unfamiliar, but recently has become increasingly popular. That is a markup language. Hierarchy is particularly simple and clear, easy to read and write, but also easy for machines to parse and generate.

golang standard library does not provide us with a temporary operating yaml standard library, but there are a lot of good on github open source third-party libraries for our use.

  1. Creating conf.yaml:
enabled: true
path: /usr/local
  1. Download third-party libraries: go get gopkg.in/yaml.v2

  2. Creating config_yaml.go:
package main

import (
    "fmt"
    "io/ioutil"
    "log"

    "gopkg.in/yaml.v2"
)

type conf struct {
    Enabled bool   `yaml:"enabled"` //yaml:yaml格式 enabled:属性的为enabled
    Path    string `yaml:"path"`
}

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("path:" + c.Path)
}

After starting the operation, the output is as follows:

D:\Go_Path\go\src\configmgr>go run config_yaml.go
path:/usr/local

Guess you like

Origin www.cnblogs.com/Paul-watermelon/p/11228008.html
Recommended