官方網(wǎng)站
https://ini.unknwon.io/docs/intro/getting_started
go get gopkg.in/ini.v1
tmp
my.ini
main.go
my.ini
# 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() {
dir, _ := os.Getwd()
fmt.Println(dir)
path := dir + "\\src\\test\\tmp\\" // windows下的路徑 需要注意修改一下 默認(rèn)是gopath
path_name := path + "my.ini"
cfg, err := ini.Load(path_name)
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
// 典型讀取操作,默認(rèn)分區(qū)可以使用空字符串表示
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"}))
// 如果讀取的值不在候選列表內(nèi),則會(huì)回退使用提供的默認(rèn)值
fmt.Println("Email Protocol:",
cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))
// 試一試自動(dòng)類型轉(zhuǎn)換
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))
// 差不多了,修改某個(gè)值然后進(jìn)行保存
cfg.Section("").Key("app_mode").SetValue("production")
cfg.SaveTo(path+"my.ini.local")
}
注意路徑
App Mode: development
Data Path: /home/git/grafana
Server Protocol: http
Email Protocol: smtp
Port Number: (int) 9999
Enforce Domain: (bool) true
分享題目:go讀取ini文件并修改
新聞來源:http://chinadenli.net/article40/ppsoeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、建站公司、全網(wǎng)營(yíng)銷推廣、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)