配置文件操作

第三方庫

gopkg.in/ini.v1

Go語言 - 配置文件操作


配置加載

創建一個空的配置

cfg := ini.Empty()

直接加載存在的配置文件,如果文件不存在就會報錯

cfg, err := ini.Load("app.ini")

可以同時加載多個配置文件,後面的配置文件鍵值會覆蓋前面一個

cfg, err := ini.Load("app.ini", "app_dev.ini")

不能夠確定其中哪些文件是不存在的,可以通過調用函數 LooseLoad() 來忽略它們。

cfg, err := ini.LooseLoad("app.ini", "app_dev.ini")

跳過無法識別的數據行

cfg, err := ini.LoadSources(ini.LoadOptions{SkipUnrecognizableLines: true}, "other.ini")
Go語言 - 配置文件操作


保存配置

比較原始的做法是輸出配置到某個文件:

err = cfg.SaveTo("app.ini")

保存時調整縮進

err = cfg.SaveToIndent("app.ini", "\\t")
Go語言 - 配置文件操作


操作分區

獲取制定分區的對象

sec, err := cfg.GetSection("db")

如果想要獲取默認分區,則可以用空字符串代替分區名:

sec, err := cfg.GetSection("")

相對應的,還可以使用 ini.DEFAULT_SECTION 來獲取默認分區:

sec, err := cfg.GetSection(ini.DEFAULT_SECTION)

當您非常確定某個分區是存在的,可以使用以下簡便方法:

sec := cfg.Section("section name")

如果不存再,會自動創建並返回一個對應的分區對象。

創建一個分區:

err := cfg.NewSection("new section")

獲取所有分區對象或名稱:

secs := cfg.Sections()
names := cfg.SectionStrings()
Go語言 - 配置文件操作


操作鍵

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.Section("").KeysHash()

操作鍵值

獲取一個類型為字符串(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, Off
v, 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() // RFC3339
v = 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.Section("").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

操作註釋

下述幾種情況的內容將被視為註釋:

所有以 # 或 ; 開頭的行

所有在 # 或 ; 之後的內容

分區標籤後的文字 (即 [分區名] 之後的內容)

如果你希望使用包含 # 或 ; 的值,請使用 ` 或 """ 進行包覆。

除此之外,還可以通過 LoadOptions 完全忽略行內註釋:

cfg, err := ini.LoadSources(ini.LoadOptions{
IgnoreInlineComment: true,
}, "app.ini")

或要求註釋符號前必須帶有一個空格:

cfg, err := ini.LoadSources(ini.LoadOptions{
SpaceBeforeInlineComment: true,
}, "app.ini")


分享到:


相關文章: