init配置文件與解析
INI配置文件有三要素
- parameters
指一條配置,就像key = value這樣的。git
- sections
sections是parameters的集合,sections必須獨佔一行而且用[]括起來。github
sections沒有明顯的結束方式,一個sections的開始就是另外一個sections的結束。sql
- comments
指INI配置文件的註釋,以 ; 開頭。shell
[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)
。session
**Package ini provides INI file read and write functionality in Go. **app
安裝
The minimum requirement of Go is 1.6.dom
$ go get gopkg.in/ini.v1
Please add -u
flag to update in the future.ide
開始使用
咱們將經過一個很是簡單的例子來了解如何使用。ui
首先,咱們須要在任意目錄建立兩個文件(my.ini
和 main.go
),在這裏咱們選擇 /tmp/ini
目錄。url
$ 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