Go 每日一庫之 go-ini

熱烈歡迎你,相識是一種緣分,Echa 哥為了你的到來特意準備了一份驚喜,go學習資料《 》


Go 每日一庫之 go-ini


簡介

ini 是 Windows 上常用的配置文件格式。MySQL 的 Windows 版就是使用 ini 格式存儲配置的。 go-ini是 Go 語言中用於操作 ini 文件的第三方庫。

本文介紹go-ini庫的使用。

快速使用

go-ini 是第三方庫,使用前需要安裝:

<code>$ go get gopkg.in/ini.v1
複製代碼/<code>

也可以使用 GitHub 上的倉庫:

<code>$ go get github.com/go-ini/ini
複製代碼/<code>

首先,創建一個my.ini配置文件:

<code>app_name = awesome web

# possible values: DEBUG, INFO, WARNING, ERROR, FATAL
log_level = DEBUG

[mysql]
ip = 127.0.0.1
port = 3306
user = dj
password = 123456
database = awesome

[redis]
ip = 127.0.0.1
port = 6381

複製代碼/<code>

使用 go-ini 庫讀取:

<code>package main

import (
"fmt"
"log"

"gopkg.in/ini.v1"
)

func main() {
cfg, err := ini.Load("my.ini")
if err != nil {
log.Fatal("Fail to read file: ", err)
}

fmt.Println("App Name:", cfg.Section("").Key("app_name").String())
fmt.Println("Log Level:", cfg.Section("").Key("log_level").String())

fmt.Println("MySQL IP:", cfg.Section("mysql").Key("ip").String())
mysqlPort, err := cfg.Section("mysql").Key("port").Int()
if err != nil {
log.Fatal(err)
}
fmt.Println("MySQL Port:", mysqlPort)
fmt.Println("MySQL User:", cfg.Section("mysql").Key("user").String())
fmt.Println("MySQL Password:", cfg.Section("mysql").Key("password").String())
fmt.Println("MySQL Database:", cfg.Section("mysql").Key("database").String())

fmt.Println("Redis IP:", cfg.Section("redis").Key("ip").String())
redisPort, err := cfg.Section("redis").Key("port").Int()
if err != nil {
log.Fatal(err)
}
fmt.Println("Redis Port:", redisPort)
}
複製代碼/<code>

在 ini 文件中,每個鍵值對佔用一行,中間使用=隔開。以#開頭的內容為註釋。ini 文件是以分區(section)組織的。 分區以[name]開始,在下一個分區前結束。所有分區前的內容屬於默認分區,如my.ini文件中的app_name和log_level。

使用go-ini讀取配置文件的步驟如下:

  • 首先調用ini.Load加載文件,得到配置對象cfg;
  • 然後以分區名調用配置對象的Section方法得到對應的分區對象section,默認分區的名字為"",也可以使用ini.DefaultSection;
  • 以鍵名調用分區對象的Key方法得到對應的配置項key對象;
  • 由於文件中讀取出來的都是字符串,key對象需根據類型調用對應的方法返回具體類型的值使用,如上面的String、MustInt方法。

運行以下程序,得到輸出:

<code>App Name: awesome web
Log Level: DEBUG
MySQL IP: 127.0.0.1
MySQL Port: 3306
MySQL User: dj
MySQL Password: 123456
MySQL Database: awesome
Redis IP: 127.0.0.1
Redis Port: 6381
複製代碼/<code>

配置文件中存儲的都是字符串,所以類型為字符串的配置項不會出現類型轉換失敗的,故String()方法只返回一個值。 但如果類型為Int/Uint/Float64這些時,轉換可能失敗。所以Int()/Uint()/Float64()返回一個值和一個錯誤。

要留意這種不一致!如果我們將配置中 redis 端口改成非法的數字 x6381,那麼運行程序將報錯:

<code>2020/01/14 22:43:13 strconv.ParseInt: parsing "x6381": invalid syntax
複製代碼/<code>

Must*便捷方法

如果每次取值都需要進行錯誤判斷,那麼代碼寫起來會非常繁瑣。為此,go-ini也提供對應的MustType(Type 為Init/Uint/Float64等)方法,這個方法只返回一個值。 同時它接受可變參數,如果類型無法轉換,取參數中第一個值返回,並且該參數設置為這個配置的值,下次調用返回這個值:

<code>package main

import (
"fmt"
"log"

"gopkg.in/ini.v1"
)

func main() {
cfg, err := ini.Load("my.ini")
if err != nil {
log.Fatal("Fail to read file: ", err)
}

redisPort, err := cfg.Section("redis").Key("port").Int()
if err != nil {
fmt.Println("before must, get redis port error:", err)
} else {
fmt.Println("before must, get redis port:", redisPort)
}

fmt.Println("redis Port:", cfg.Section("redis").Key("port").MustInt(6381))

redisPort, err = cfg.Section("redis").Key("port").Int()

if err != nil {
fmt.Println("after must, get redis port error:", err)
} else {
fmt.Println("after must, get redis port:", redisPort)
}
}
複製代碼/<code>

配置文件還是 redis 端口為非數字 x6381 時的狀態,運行程序:

<code>before must, get redis port error: strconv.ParseInt: parsing "x6381": invalid syntax
redis Port: 6381
after must, get redis port: 6381
複製代碼/<code>

我們看到第一次調用Int返回錯誤,以 6381 為參數調用MustInt之後,再次調用Int,成功返回 6381。MustInt源碼也比較簡單:

<code>// gopkg.in/ini.v1/key.go
func (k *Key) MustInt(defaultVal ...int) int {
val, err := k.Int()
if len(defaultVal) > 0 && err != nil {
k.value = strconv.FormatInt(int64(defaultVal[0]), 10)
return defaultVal[0]
}
return val
}
複製代碼/<code>

分區操作

獲取信息

在加載配置之後,可以通過Sections方法獲取所有分區,SectionStrings()方法獲取所有分區名。

<code>sections := cfg.Sections()
names := cfg.SectionStrings()

fmt.Println("sections: ", sections)
fmt.Println("names: ", names)

複製代碼/<code>

運行輸出 3 個分區:

<code>[DEFAULT mysql redis]
複製代碼/<code>

調用Section(name)獲取名為name的分區,如果該分區不存在,則自動創建一個分區返回:

<code>newSection := cfg.Section("new")

fmt.Println("new section: ", newSection)
fmt.Println("names: ", cfg.SectionStrings())
複製代碼/<code>

創建之後調用SectionStrings方法,新分區也會返回:

<code>names:  [DEFAULT mysql redis new]
複製代碼/<code>

也可以手動創建一個新分區,如果分區已存在,則返回錯誤:

<code>err := cfg.NewSection("new")
複製代碼/<code>

父子分區

在配置文件中,可以使用佔位符%(name)s表示用之前已定義的鍵name的值來替換,這裡的s表示值為字符串類型:

<code>NAME = ini
VERSION = v1
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s

[package]
CLONE_URL = https://%(IMPORT_PATH)s


[package.sub]
複製代碼/<code>

上面在默認分區中設置IMPORT_PATH的值時,使用了前面定義的NAME和VERSION。 在package分區中設置CLONE_URL的值時,使用了默認分區中定義的IMPORT_PATH。

我們還可以在分區名中使用.表示兩個或多個分區之間的父子關係,例如package.sub的父分區為package,package的父分區為默認分區。 如果某個鍵在子分區中不存在,則會在它的父分區中再次查找,直到沒有父分區為止:

<code>cfg, err := ini.Load("parent_child.ini")
if err != nil {
fmt.Println("Fail to read file: ", err)
return
}

fmt.Println("Clone url from package.sub:", cfg.Section("package.sub").Key("CLONE_URL").String())
複製代碼/<code>

運行程序輸出:

<code>Clone url from package.sub: https://gopkg.in/ini.v1
複製代碼/<code>

子分區中package.sub中沒有鍵CLONE_URL,返回了父分區package中的值。

保存配置

有時候,我們需要將生成的配置寫到文件中。例如在寫工具的時候。保存有兩種類型的接口,一種直接保存到文件,另一種寫入到io.Writer中:

<code>err = cfg.SaveTo("my.ini")
err = cfg.SaveToIndent("my.ini", "\\t")

cfg.WriteTo(writer)
cfg.WriteToIndent(writer, "\\t")
複製代碼/<code>

下面我們通過程序生成前面使用的配置文件my.ini並保存:

<code>package main

import (
"fmt"
"os"

"gopkg.in/ini.v1"
)

func main() {
cfg := ini.Empty()

defaultSection := cfg.Section("")
defaultSection.NewKey("app_name", "awesome web")
defaultSection.NewKey("log_level", "DEBUG")

mysqlSection, err := cfg.NewSection("mysql")
if err != nil {
fmt.Println("new mysql section failed:", err)
return
}
mysqlSection.NewKey("ip", "127.0.0.1")
mysqlSection.NewKey("port", "3306")
mysqlSection.NewKey("user", "root")
mysqlSection.NewKey("password", "123456")
mysqlSection.NewKey("database", "awesome")

redisSection, err := cfg.NewSection("redis")
if err != nil {
fmt.Println("new redis section failed:", err)
return
}
redisSection.NewKey("ip", "127.0.0.1")
redisSection.NewKey("port", "6381")

err = cfg.SaveTo("my.ini")
if err != nil {
fmt.Println("SaveTo failed: ", err)

}

err = cfg.SaveToIndent("my-pretty.ini", "\\t")
if err != nil {
fmt.Println("SaveToIndent failed: ", err)
}

cfg.WriteTo(os.Stdout)
fmt.Println()
cfg.WriteToIndent(os.Stdout, "\\t")
}
複製代碼/<code>

運行程序,生成兩個文件my.ini和my-pretty.ini,同時控制檯輸出文件內容。

my.ini:

<code>app_name  = awesome web
log_level = DEBUG

[mysql]
ip = 127.0.0.1
port = 3306
user = root
password = 123456
database = awesome

[redis]
ip = 127.0.0.1
port = 6381
複製代碼/<code>

my-pretty.ini:

<code>app_name  = awesome web
log_level = DEBUG

[mysql]
\tip = 127.0.0.1
\tport = 3306
\tuser = root
\tpassword = 123456
\tdatabase = awesome

[redis]
\tip = 127.0.0.1
\tport = 6381

複製代碼/<code>

*Indent方法會對子分區下的鍵增加縮進,看起來美觀一點。

分區與結構體字段映射

定義結構變量,加載完配置文件後,調用MapTo將配置項賦值到結構變量的對應字段中。

<code>package main

import (
"fmt"

"gopkg.in/ini.v1"
)

type Config struct {
AppName string `ini:"app_name"`
LogLevel string `ini:"log_level"`

MySQL MySQLConfig `ini:"mysql"`
Redis RedisConfig `ini:"redis"`
}

type MySQLConfig struct {
IP string `ini:"ip"`
Port int `ini:"port"`
User string `ini:"user"`
Password string `ini:"password"`
Database string `ini:"database"`
}

type RedisConfig struct {
IP string `ini:"ip"`
Port int `ini:"port"`
}

func main() {
cfg, err := ini.Load("my.ini")
if err != nil {
fmt.Println("load my.ini failed: ", err)
}

c := Config{}
cfg.MapTo(&c)

fmt.Println(c)
}
複製代碼/<code>

MapTo內部使用了反射,所以結構體字段必須都是導出的。如果鍵名與字段名不相同,那麼需要在結構標籤中指定對應的鍵名。 這一點與 Go 標準庫encoding/json和encoding/xml不同。標準庫json/xml解析時可以將鍵名app_name對應到字段名AppName。 或許這是go-ini庫可以優化的點?

先加載,再映射有點繁瑣,直接使用ini.MapTo將兩步合併:

<code>err = ini.MapTo(&c, "my.ini")
複製代碼/<code>

也可以只映射一個分區:

<code>mysqlCfg := MySQLConfig{}
err = cfg.Section("mysql").MapTo(&mysqlCfg)
複製代碼/<code>

還可以通過結構體生成配置:

<code>cfg := ini.Empty()

c := Config {
AppName: \t"awesome web",
LogLevel: \t"DEBUG",
MySQL: MySQLConfig {
IP: \t"127.0.0.1",
Port:\t3306,
User:\t"root",
Password:"123456",
Database:"awesome",

},
Redis: RedisConfig {
IP:\t\t"127.0.0.1",
Port:\t6381,
},
}

err := ini.ReflectFrom(cfg, &c)
if err != nil {
fmt.Println("ReflectFrom failed: ", err)
return
}

err = cfg.SaveTo("my-copy.ini")
if err != nil {
fmt.Println("SaveTo failed: ", err)
return
}
複製代碼/<code>

總結

本文介紹了go-ini庫的基本用法和一些有趣的特性。示例代碼已上傳GitHub。 其實go-ini還有很多高級特性。官方文檔非常詳細,推薦去看,而且有中文喲~ 作者無聞,相信做 Go 開發的都不陌生。

參考

  1. go-ini GitHub 倉庫
  2. go-ini 官方文檔


分享到:


相關文章: