# ini_reader **Repository Path**: curious_Li/ini_reader ## Basic Information - **Project Name**: ini_reader - **Description**: No description available - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ini_reader 本包是一个简单的配置文件读取包,采用 go 语言实现,参考了完整版 ini 读写包—[Github](https://github.com/go-ini/ini) ## 功能 * .ini 文件读取 * 监听 .ini 文件的改动,并在改动后加载 ## 安装 ``` go get gitee.com/curious_Li/ini_reader ``` ## 使用样例 *( 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 ```go package main import ( "fmt" "gitee.com/curious_Li/ini_reader" ) func main() { // Load original .ini file cfg := ini_reader.Load("my.ini") fmt.Println(".app_mode : ", cfg.Section("").Key("app_mode")) fmt.Println("paths.data : ", cfg.Section("paths").Key("data")) fmt.Println("server.protocol : ", cfg.Section("server").Key("protocol")) fmt.Println("server.http_port : ", cfg.Section("server").Key("http_port")) fmt.Println("server.enforce_domain : ", cfg.Section("server").Key("enforce_domain")) // Watch for changes of .ini file var fl ini_reader.FileListener cfg_w, err := ini_reader.Watch(fl, "my.ini") if err != nil { panic(err) } fmt.Println("After changes :") fmt.Println(".app_mode : ", cfg_w.Section("").Key("app_mode")) fmt.Println("paths.data : ", cfg_w.Section("paths").Key("data")) fmt.Println("server.protocol : ", cfg_w.Section("server").Key("protocol")) fmt.Println("server.http_port : ", cfg_w.Section("server").Key("http_port")) fmt.Println("server.enforce_domain : ", cfg_w.Section("server").Key("enforce_domain")) } ``` 在所在目录下运行 `$ go run main.go` 然后修改 my.ini 并保存,我选择将 http_port 改为 6666,enforce_domain 改为 false 得到结果,符合预期 ![example](imgs/example.PNG) ## API文档 使用 godoc 生成的 html 文档 [API](./api.html) ``` $ cd $GOPATH/src/gitee.com/curious_Li/ini_reader $ go doc $ godoc -url="pkg/gitee.com/curious_Li/ini_reader" > api.html ```