# initoy **Repository Path**: barytes/initoy ## Basic Information - **Project Name**: initoy - **Description**: No description available - **Primary Language**: Go - **License**: MulanPSL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-22 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # INITOY initoy包提供了Linux和Windows平台下监听ini文件改动并读取ini文件的功能 此包是对 gopkg.in/ini 包的简单模仿。 ## 安装 通过 go get 命令安装: ``` $ go get gitee.com/barytes/initoy ``` 也可以直接将仓库克隆至 `GOPATH/src/ ` 路径下安装: ``` $ git clone https://gitee.com/barytes/initoy.git $GOPATH/src/gitee.com/barytes/initoy/ ``` ## 开始使用 假定路径中已经存在一个如下的 demo.ini 文件: ```ini 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 ``` 如下的两个示例程序可以展示 initoy 包的用法: ### ini文件操作 ```go func main() { cfg, err := initoy.Load("./demo.ini") if err != nil { fmt.Printf("Fail to read file: %v", err) os.Exit(1) } //按名称获取一个Section sec := cfg.Section("paths") fmt.Println(sec.Name()) //按名称获取一个键值对 key := sec.Key("data") fmt.Println(key.Name(), key.Value()) fmt.Println("=======================================================") //打印整个配置 fmt.Println(cfg.String()) } ``` 可以得到如下输出: ``` (base) [root@VM-0-12-centos a]# go run main.go paths data /home/git/grafana ======================================================= source file: ./demo.ini [Default] 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 = https # 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 ``` ### 监听 ```go package main import ( "fmt" "os" "time" "github.com/user/initoy" ) func listenInstance(filePath string) { initialStat, err := os.Stat(filePath) if err != nil { panic(err) } for { stat, err := os.Stat(filePath) if err != nil { panic(err) } if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() { break } time.Sleep(1 * time.Second) } } func main() { //自定义监听函数 var f initoy.ListenFunc = listenInstance //监听文件改动 cfg, err := initoy.Watch("./demo.ini", f) if err != nil{ panic(err) } fmt.Println(cfg.String()) } ``` 在运行程序后,将`demo.ini` 文件的`http_port = 9999` 行改为`http_port = 999999`,得到输出如下: ``` (base) [root@VM-0-12-centos a]# go run main.go source file: ./demo.ini [Default] 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 = https # The http port to use http_port = 999999 # Redirect to correct domain if host header does not match domain # Prevents DNS rebinding attacks enforce_domain = true ``` ## API文档 使用浏览器打开API.html,或使用`go doc -all `命令查看: ``` $ go doc -all initoy ```