init配置文件與解析
INI配置文件有三要素
- parameters
指一條配置,就像key = value這樣的。
- sections
sections是parameters的集合,sections必須獨占一行並且用[]括起來。
sections沒有明顯的結束方式,一個sections的開始就是另一個sections的結束。
- comments
指INI配置文件的注釋,以 ; 開頭。
[DataBase]
ServerIP=**********
ServerPort=8080
ControlConnectString=QWDJ7+XH6oWaANAGhVgh5/5UxYrA2rfz/ufAkDlN1H9Tw+v7Z0SoCfR+wYdyzCjF/ANUfPxlO6cLDAhm4xxmbADyKs6zmkWuGQNgDZmPx6c=
ControlConnectCategory=0
[LogonInfo]
SaveUserID=Y
UserID=admin
DBServer=AppDB
DBCenter=Demo
[UserConfig]
OpenDownloadFileAtOnec=Y
WindowStyle=DevExpress Dark Style
[Language]
Language=CHS
[AutoUpdate]
Version=1.1
init 配置文件的解析
這里我們使用GitHub上的第三方庫(https://github.com/go-ini)
。
**Package ini provides INI file read and write functionality in Go. **
安裝
The minimum requirement of Go is 1.6.
$ go get gopkg.in/ini.v1
Please add -u
flag to update in the future.
開始使用
我們將通過一個非常簡單的例子來了解如何使用。
首先,我們需要在任意目錄創建兩個文件(my.ini
和 main.go
),在這里我們選擇 /tmp/ini
目錄。
$ mkdir -p /tmp/ini
$ cd /tmp/ini
$ touch my.ini main.go
$ tree .
.
├── main.go
└── my.ini
0 directories, 2 files
現在,我們編輯 my.ini
文件並輸入以下內容(部分內容來自 Grafana)。
# possible values : production, development
app_mode = development
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana
[server]
# Protocol (http or https)
protocol = http
# The http port to use
http_port = 9999
# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true
很好,接下來我們需要編寫 main.go
文件來操作剛才創建的配置文件。
package main
import (
"fmt"
"os"
"gopkg.in/ini.v1"
)
func main() {
cfg, err := ini.Load("my.ini")//初始化一個cfg
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// 典型讀取操作,默認分區可以使用空字符串表示
fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String())
fmt.Println("Data Path:", cfg.Section("paths").Key("data").String())
// 我們可以做一些候選值限制的操作
fmt.Println("Server Protocol:",
cfg.Section("server").Key("protocol").In("http", []string{"http", "https"}))
// 如果讀取的值不在候選列表內,則會回退使用提供的默認值
fmt.Println("Email Protocol:",
cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))
// 試一試自動類型轉換
fmt.Printf("Port Number: (%[1]T) %[1]d\n", cfg.Section("server").Key("http_port").MustInt(9999))
fmt.Printf("Enforce Domain: (%[1]T) %[1]v\n", cfg.Section("server").Key("enforce_domain").MustBool(false))
// 差不多了,修改某個值然后進行保存
cfg.Section("").Key("app_mode").SetValue("production")
cfg.SaveTo("my.ini.local")
}
運行程序,我們可以看下以下輸出
$ go run main.go
App Mode: development
Data Path: /home/git/grafana
Server Protocol: http
Email Protocol: smtp
Port Number: (int) 9999
Enforce Domain: (bool) true
$ cat my.ini.local
# possible values : production, development
app_mode = production
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
data = /home/git/grafana
...
高級用法
將配制文件映射到結構體
想要使用更加面向對象的方式玩轉 INI 嗎?好主意。
配置文件如下:
Name = Unknwon
age = 21
Male = true
Born = 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"`//這里需要用到反射,因為和ini文件的字段不同
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",
}
// ..
將結構體映射成配置文件
type Embeded struct {
Dates []time.Time `delim:"|" comment:"Time data"`
Places []string `ini:"places,omitempty"`
None []int `ini:",omitempty"`
}
type Author struct {
Name string `ini:"NAME"`
Male bool
Age int `comment:"Author's age"`
GPA float64
NeverMind string `ini:"-"`
*Embeded `comment:"Embeded section"`
}
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 = Unknwon
Male = true
; Author's age
Age = 21
GPA = 2.8
; Embeded section
[Embeded]
; Time data
Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
places = HangZhou,Boston