Git 專案推薦 | Go 語言讀寫 INI 檔案工具包
原文 http://git.oschina.net/Unknown/ini
主題 Git Go語言
本包提供了 Go 語言中讀寫 INI 檔案的功能。
功能特性
- 支援覆蓋載入多個數據源(
[]byte
或檔案) - 支援遞迴讀取鍵值
- 支援讀取父子分割槽
- 支援讀取自增鍵名
- 支援讀取多行的鍵值
- 支援大量輔助方法
- 支援在讀取時直接轉換為 Go 語言型別
- 支援讀取和 寫入 分割槽和鍵的註釋
- 輕鬆操作分割槽、鍵值和註釋
- 在儲存檔案時分割槽和鍵值會保持原有的順序
下載安裝
使用一個特定版本:
go get gopkg.in/ini.v1
使用最新版:
go get github.com/go-ini/ini
如需更新請新增 -u
測試安裝
如果您想要在自己的機器上執行測試,請使用 -t
標記:
go get -t gopkg.in/ini.v1
如需更新請新增 -u
選項。
開始使用
從資料來源載入
一個 資料來源 可以是 []byte
型別的原始資料,或 string
型別的檔案路徑。您可以載入 任意多個 資料來源。如果您傳遞其它型別的資料來源,則會直接返回錯誤。
cfg, err := ini.Load([]byte("raw data"), "filename")
或者從一個空白的檔案開始:
cfg := ini.Empty()
當您在一開始無法決定需要載入哪些資料來源時,仍可以使用 Append()
err := cfg.Append("other file", []byte("other raw data"))
操作分割槽(Section)
獲取指定分割槽:
section, err := cfg.GetSection("section name")
如果您想要獲取預設分割槽,則可以用空字串代替分割槽名:
section, err := cfg.GetSection("")
當您非常確定某個分割槽是存在的,可以使用以下簡便方法:
section := cfg.Section("")
如果不小心判斷錯了,要獲取的分割槽其實是不存在的,那會發生什麼呢?沒事的,它會自動建立並返回一個對應的分割槽物件給您。
建立一個分割槽:
err := cfg.NewSection("new section")
獲取所有分割槽物件或名稱:
sections := cfg.Sections()
names := cfg.SectionStrings()
操作鍵(Key)
獲取某個分割槽下的鍵:
key, err := cfg.Section("").GetKey("key name")
和分割槽一樣,您也可以直接獲取鍵而忽略錯誤處理:
key := cfg.Section("").Key("key name")
判斷某個鍵是否存在:
yes := cfg.Section("").HasKey("key name")
建立一個新的鍵:
err := cfg.Section("").NewKey("name", "value")
獲取分割槽下的所有鍵或鍵名:
keys := cfg.Section("").Keys()
names := cfg.Section("").KeyStrings()
獲取分割槽下的所有鍵值對的克隆:
hash := cfg.GetSection("").KeysHash()
操作鍵值(Value)
獲取一個型別為字串(string)的值:
val := cfg.Section("").Key("key name").String()
獲取值的同時通過自定義函式進行處理驗證:
val := cfg.Section("").Key("key name").Validate(func(in string) string { if len(in) == 0 { return "default"
} return in})
如果您不需要任何對值的自動轉變功能(例如遞迴讀取),可以直接獲取原值(這種方式效能最佳):
val := cfg.Section("").Key("key name").Value()
判斷某個原值是否存在:
yes := cfg.Section("").HasValue("test value")
獲取其它型別的值:
// 布林值的規則:// true 當值為:1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On// false 當值為:0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Offv, err = cfg.Section("").Key("BOOL").Bool()
v, err = cfg.Section("").Key("FLOAT64").Float64()
v, err = cfg.Section("").Key("INT").Int()
v, err = cfg.Section("").Key("INT64").Int64()
v, err = cfg.Section("").Key("UINT").Uint()
v, err = cfg.Section("").Key("UINT64").Uint64()
v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339)
v, err = cfg.Section("").Key("TIME").Time() // RFC3339v = cfg.Section("").Key("BOOL").MustBool()
v = cfg.Section("").Key("FLOAT64").MustFloat64()
v = cfg.Section("").Key("INT").MustInt()
v = cfg.Section("").Key("INT64").MustInt64()
v = cfg.Section("").Key("UINT").MustUint()
v = cfg.Section("").Key("UINT64").MustUint64()
v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339)
v = cfg.Section("").Key("TIME").MustTime() // RFC3339// 由 Must 開頭的方法名允許接收一個相同型別的引數來作為預設值,// 當鍵不存在或者轉換失敗時,則會直接返回該預設值。// 但是,MustString 方法必須傳遞一個預設值。v = cfg.Seciont("").Key("String").MustString("default")
v = cfg.Section("").Key("BOOL").MustBool(true)
v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25)
v = cfg.Section("").Key("INT").MustInt(10)
v = cfg.Section("").Key("INT64").MustInt64(99)
v = cfg.Section("").Key("UINT").MustUint(3)
v = cfg.Section("").Key("UINT64").MustUint64(6)
v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now())
v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339
如果我的值有好多行怎麼辦?
[advance]
ADDRESS = """404 road,
NotFound, State, 5000
Earth"""
嗯哼?小 case!
cfg.Section("advance").Key("ADDRESS").String()/* --- start ---
404 road,
NotFound, State, 5000
Earth
------ end --- */
贊爆了!那要是我屬於一行的內容寫不下想要寫到第二行怎麼辦?
[advance]
two_lines = how about
continuation lines?
lots_of_lines = 1
2
3
4
簡直是小菜一碟!
cfg.Section("advance").Key("two_lines").String() // how about continuation lines?cfg.Section("advance").Key("lots_of_lines").String() // 1 2 3 4
需要注意的是,值兩側的單引號會被自動剔除:
foo = "some value" // foo: some valuebar = 'some value' // bar: some value
這就是全部了?哈哈,當然不是。
操作鍵值的輔助方法
獲取鍵值時設定候選值:
v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"})v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75})v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30})v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30})v = cfg.Section("").Key("UINT").InUint(4, []int{3, 6, 9})v = cfg.Section("").Key("UINT64").InUint64(8, []int64{3, 6, 9})v = cfg.Section("").Key("TIME").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time1, time2, time3})v = cfg.Section("").Key("TIME").InTime(time.Now(), []time.Time{time1, time2, time3}) // RFC3339
如果獲取到的值不是候選值的任意一個,則會返回預設值,而預設值不需要是候選值中的一員。
驗證獲取的值是否在指定範圍內:
vals = cfg.Section("").Key("FLOAT64").RangeFloat64(0.0, 1.1, 2.2)vals = cfg.Section("").Key("INT").RangeInt(0, 10, 20)vals = cfg.Section("").Key("INT64").RangeInt64(0, 10, 20)vals = cfg.Section("").Key("UINT").RangeUint(0, 3, 9)vals = cfg.Section("").Key("UINT64").RangeUint64(0, 3, 9)vals = cfg.Section("").Key("TIME").RangeTimeFormat(time.RFC3339, time.Now(), minTime, maxTime)vals = cfg.Section("").Key("TIME").RangeTime(time.Now(), minTime, maxTime) // RFC3339
自動分割鍵值到切片(slice)
當存在無效輸入時,使用零值代替:
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]// Input: how, 2.2, are, you -> [0.0 2.2 0.0 0.0]vals = cfg.Section("").Key("STRINGS").Strings(",")
vals = cfg.Section("").Key("FLOAT64S").Float64s(",")
vals = cfg.Section("").Key("INTS").Ints(",")
vals = cfg.Section("").Key("INT64S").Int64s(",")
vals = cfg.Section("").Key("UINTS").Uints(",")
vals = cfg.Section("").Key("UINT64S").Uint64s(",")
vals = cfg.Section("").Key("TIMES").Times(",")
從結果切片中剔除無效輸入:
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]// Input: how, 2.2, are, you -> [2.2]vals = cfg.Section("").Key("FLOAT64S").ValidFloat64s(",")
vals = cfg.Section("").Key("INTS").ValidInts(",")
vals = cfg.Section("").Key("INT64S").ValidInt64s(",")
vals = cfg.Section("").Key("UINTS").ValidUints(",")
vals = cfg.Section("").Key("UINT64S").ValidUint64s(",")
vals = cfg.Section("").Key("TIMES").ValidTimes(",")
當存在無效輸入時,直接返回錯誤:
// Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]// Input: how, 2.2, are, you -> errorvals = cfg.Section("").Key("FLOAT64S").StrictFloat64s(",")
vals = cfg.Section("").Key("INTS").StrictInts(",")
vals = cfg.Section("").Key("INT64S").StrictInt64s(",")
vals = cfg.Section("").Key("UINTS").StrictUints(",")
vals = cfg.Section("").Key("UINT64S").StrictUint64s(",")
vals = cfg.Section("").Key("TIMES").StrictTimes(",")
儲存配置
終於到了這個時刻,是時候儲存一下配置了。
比較原始的做法是輸出配置到某個檔案:
// ...err = cfg.SaveTo("my.ini")
err = cfg.SaveToIndent("my.ini", "t")
另一個比較高階的做法是寫入到任何實現 io.Writer
介面的物件中:
// ...cfg.WriteTo(writer)
cfg.WriteToIndent(writer, "t")
高階用法
遞迴讀取鍵值
在獲取所有鍵值的過程中,特殊語法 %(<name>)s
會被應用,其中 <name>
可以是相同分割槽或者預設分割槽下的鍵名。字串 %(<name>)s
會被相應的鍵值所替代,如果指定的鍵不存在,則會用空字串替代。您可以最多使用 99 層的遞迴巢狀。
NAME = ini[author]NAME = UnknwonGITHUB = https://github.com/%(NAME)s[package]FULL_NAME = github.com/go-ini/%(NAME)s
cfg.Section("author").Key("GITHUB").String() // https://github.com/Unknwoncfg.Section("package").Key("FULL_NAME").String() // github.com/go-ini/ini
讀取父子分割槽
您可以在分割槽名稱中使用 .
來表示兩個或多個分割槽之間的父子關係。如果某個鍵在子分割槽中不存在,則會去它的父分割槽中再次尋找,直到沒有父分割槽為止。
NAME = iniVERSION = v1IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s[package]CLONE_URL = https://%(IMPORT_PATH)s[package.sub]
cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1
讀取自增鍵名
如果資料來源中的鍵名為 -
,則認為該鍵使用了自增鍵名的特殊語法。計數器從 1 開始,並且分割槽之間是相互獨立的。
[features]
-: Support read/write comments of keys and sections
-: Support auto-increment of key names
-: Support load multiple files to overwrite key values
cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"}
對映到結構
想要使用更加面向物件的方式玩轉 INI 嗎?好主意。
Name = Unknwonage = 21Male = trueBorn = 1993-01-01T20:17:05Z[Note]Content = Hi is a good man!Cities = HangZhou, Boston
type Note struct {
Content string
Cities []string}
type Person struct {
Name string
Age int `ini:"age"`
Male bool
Born time.Time
Note
Created time.Time `ini:"-"`
}
func main() {
cfg, err := ini.Load("path/to/ini") // ...
p := new(Person)
err = cfg.MapTo(p) // ...
// 一切竟可以如此的簡單。
err = ini.MapTo(p, "path/to/ini") // ...
// 嗯哼?只需要對映一個分割槽嗎?
n := new(Note)
err = cfg.Section("Note").MapTo(n) // ...}
結構的欄位怎麼設定預設值呢?很簡單,只要在對映之前對指定欄位進行賦值就可以了。如果鍵未找到或者型別錯誤,該值不會發生改變。
// ...p := &Person{
Name: "Joe",
}// ...
這樣玩 INI 真的好酷啊!然而,如果不能還給我原來的配置檔案,有什麼卵用?
從結構反射
可是,我有說不能嗎?
type Embeded struct {
Dates []time.Time `delim:"|"`
Places []string
None []int}
type Author struct {
Name string `ini:"NAME"`
Male bool
Age int
GPA float64
NeverMind string `ini:"-"`
*Embeded
}
func main() {
a := &Author{"Unknwon", true, 21, 2.8, "",
&Embeded{
[]time.Time{time.Now(), time.Now()},
[]string{"HangZhou", "Boston"},
[]int{},
}}
cfg := ini.Empty()
err = ini.ReflectFrom(cfg, a) // ...}
瞧瞧,奇蹟發生了。
NAME = UnknwonMale = trueAge = 21GPA = 2.8[Embeded]Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00Places = HangZhou,BostonNone =
名稱對映器(Name Mapper)
為了節省您的時間並簡化程式碼,本庫支援型別為 NameMapper
的名稱對映器,該對映器負責結構欄位名與分割槽名和鍵名之間的對映。
目前有 2 款內建的對映器:
-
AllCapsUnderscore
:該對映器將欄位名轉換至格式ALL_CAPS_UNDERSCORE
後再去匹配分割槽名和鍵名。 -
TitleUnderscore
:該對映器將欄位名轉換至格式title_underscore
後再去匹配分割槽名和鍵名。
使用方法:
type Info struct{
PackageName string}
func main() {
err = ini.MapToWithMapper(&Info{}, ini.TitleUnderscore, []byte("package_name=ini")) // ...
cfg, err := ini.Load([]byte("PACKAGE_NAME=ini")) // ...
info := new(Info)
cfg.NameMapper = ini.AllCapsUnderscore
err = cfg.MapTo(info) // ...}
使用函式 ini.ReflectFromWithMapper
時也可應用相同的規則。
對映/反射的其它說明
任何嵌入的結構都會被預設認作一個不同的分割槽,並且不會自動產生所謂的父子分割槽關聯:
type Child struct {
Age string}
type Parent struct {
Name string
Child
}
type Config struct {
City string
Parent
}
示例配置檔案:
City = Boston[Parent]Name = Unknwon[Child]Age = 21
很好,但是,我就是要嵌入結構也在同一個分割槽。好吧,你爹是李剛!
type Child struct {
Age string}
type Parent struct {
Name string
Child `ini:"Parent"`
}
type Config struct {
City string
Parent
}
示例配置檔案:
City = Boston[Parent]Name = UnknwonAge = 21
獲取幫助
- API 文件
- 建立工單
常見問題
欄位 BlockMode
是什麼?
預設情況下,本庫會在您進行讀寫操作時採用鎖機制來確保資料時間。但在某些情況下,您非常確定只進行讀操作。此時,您可以通過設定 cfg.BlockMode = false
來將讀操作提升大約 50-70% 的效能。
為什麼要寫另一個 INI 解析庫?
許多人都在使用我的 goconfig 來完成對 INI 檔案的操作,但我希望使用更加 Go 風格的程式碼。並且當您設定 cfg.BlockMode = false
時,會有大約 10-30% 的效能提升。
為了做出這些改變,我必須對 API 進行破壞,所以新開一個倉庫是最安全的做法。除此之外,本庫直接使用 gopkg.in
來進行版本化釋出。(其實真相是匯入路徑更短了)