Go语言-配置文件

摘要

Go语言读取配置文件的方式

  • yaml
  • json
  • ini
  • xml

yaml

https://www.cnblogs.com/zhaof/p/8955332.html

1
2
enabled: true
path: /usr/local

github.com/kylelemons/go-gypsy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
"fmt"

"github.com/kylelemons/go-gypsy/yaml"
)

func main() {
config, err := yaml.ReadFile("conf.yaml")
if err != nil {
fmt.Println(err)
}
fmt.Println(config.Get("path"))
fmt.Println(config.GetBool("enabled"))
}

gopkg.in/yaml.v2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

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

"gopkg.in/yaml.v2"
)

type conf struct {
Enabled bool `yaml:"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(c)
}

test1

1
2
3
4
5
6
7
8
9
cache:
enable : false
list : [redis,mongoDB]
mysql:
user : root
password : Tech2501
host : 10.11.22.33
port : 3306
name : cwi

test2

1
2
3
4
5
6
7
enable : false
list : [redis,mongoDB]
user : root
password : Tech2501
host : 10.11.22.33
port : 3306
name : cwi

module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package module
// Yaml struct of yaml
type Yaml struct {
Mysql struct {
User string `yaml:"user"`
Host string `yaml:"host"`
Password string `yaml:"password"`
Port string `yaml:"port"`
Name string `yaml:"name"`
}
Cache struct {
Enable bool `yaml:"enable"`
List []string `yaml:"list,flow"`
}
}

// Yaml1 struct of yaml
type Yaml1 struct {
SQLConf Mysql `yaml:"mysql"`
CacheConf Cache `yaml:"cache"`
}

// Yaml2 struct of yaml
type Yaml2 struct {
Mysql `yaml:"mysql,inline"`
Cache `yaml:"cache,inline"`
}

// Mysql struct of mysql conf
type Mysql struct {
User string `yaml:"user"`
Host string `yaml:"host"`
Password string `yaml:"password"`
Port string `yaml:"port"`
Name string `yaml:"name"`
}

// Cache struct of cache conf
type Cache struct {
Enable bool `yaml:"enable"`
List []string `yaml:"list,flow"`
}

main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main
import (
"io/ioutil"
"log"
"module"
yaml "gopkg.in/yaml.v2"
)
func main() {
// resultMap := make(map[string]interface{})
conf := new(module.Yaml)
yamlFile, err := ioutil.ReadFile("test.yaml")

// conf := new(module.Yaml1)
// yamlFile, err := ioutil.ReadFile("test.yaml")

// conf := new(module.Yaml2)
// yamlFile, err := ioutil.ReadFile("test1.yaml")

log.Println("yamlFile:", yamlFile)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, conf)
// err = yaml.Unmarshal(yamlFile, &resultMap)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
log.Println("conf", conf)
// log.Println("conf", resultMap)
}

github.com/ghodss/yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
"fmt"

"github.com/ghodss/yaml"
)

func main() {
j := []byte(`{"name": "John", "age": 30}`)
y, err := yaml.JSONToYAML(j)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(y))
/* Output:
name: John
age: 30
*/
j2, err := yaml.YAMLToJSON(y)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(j2))
/* Output:
{"age":30,"name":"John"}
*/
}

JSON

1
2
3
4
{
"enabled": true,
"path": "/usr/local"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

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

type configuration struct {
Enabled bool
Path string
}

func main() {
file, _ := os.Open("conf.json")
defer file.Close()

decoder := json.NewDecoder(file)
conf := configuration{}
err := decoder.Decode(&conf)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println(conf.Path)
}

xml

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8" ?>
<Config>
<enabled>true</enabled>
<path>/usr/local</path>
</Config>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

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

type configuration struct {
Enabled bool `xml:"enabled"`
Path string `xml:"path"`
}

func main() {
xmlFile, err := os.Open("conf.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()

var conf configuration
if err := xml.NewDecoder(xmlFile).Decode(&conf); err != nil {
fmt.Println("Error Decode file:", err)
return
}

fmt.Println(conf.Enabled)
fmt.Println(conf.Path)

}

ini

1
2
3
4
; A comment line
[Section]
enabled = true
path = /usr/local # another comment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
"fmt"

"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)
}