go语言解析配置文件案例
Go基础学习-使用gopkg.in/ini.v1实现文件解析及手动实现ini文件解析
ini 是 Windows 上常用的配置文件格式。MySQL 的 Windows 版就是使用 ini 格式存储配置的。go-ini 是 Go 语言中用于操作 ini 文件的第三方库。
QQ群:397745473
1 2 3 4 Go基础学习-使用gopkg.in/ini.v1实现文件解析及手动实现ini文件解析 参考: https://blog.csdn.net/wzb_wzt/article/details/107419806 参考: https://blog.csdn.net/darjun/article/details/103998569 官方文档:https://ini.unknwon.cn/docs
1 2 安装软件包: go get -u github.com/go -ini/ini
参考案例1 代码如下:
my.ini :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //配置文件 # 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
x.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package mainimport ( "fmt" "os" "gopkg.in/ini.v1" ) func main () { cfg, err := ini.Load("my.ini" ) 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" ) }
配置文件读取经典代码片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 # 参考:https: package configimport ( "log" "os" "gopkg.in/ini.v1" ) type ConfigList struct { FgiAPIKey string FgiAPIHost string Port int JwtAccess string JwtRefresh string } var Config ConfigListfunc init () { cfg, err := ini.Load("config.ini" ) if err != nil { log.Printf("Failed to read file: %v" , err) os.Exit(1 ) } Config = ConfigList{ FgiAPIKey: cfg.Section("fgi" ).Key("api_key" ).String(), FgiAPIHost: cfg.Section("fgi" ).Key("api_host" ).String(), Port: cfg.Section("web" ).Key("port" ).MustInt(), JwtAccess: cfg.Section("secret" ).Key("jwt_access" ).String(), JwtRefresh: cfg.Section("secret" ).Key("jwt_refresh" ).String(), } }
参考案例2 main.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package mainimport ( "fmt" "os" "github.com/go-ini/ini" ) type config struct { email string key string } var cfg configfunc init () { configFile, err := ini.Load("./config.ini" ) if err != nil { fmt.Printf("Fail to read file: %v" , err) os.Exit(1 ) } cfg.email = configFile.Section("fofa" ).Key("email" ).String() cfg.key = configFile.Section("fofa" ).Key("key" ).String() } func main () { fmt.Println(cfg.email) fmt.Println(cfg.key) }
./config.ini
1 2 3 [fofa] email = `xxxx@xx.com`key = `xxxxxxxxxxxxxxxxxxxxxxxx`
常见问题解决 执行文件时提示找不到包的解决办法:
1 2 go run testConf.go testConf.go:9:2: no required module provides package gopkg.in/ini.v1: go.mod file not found in current directory or any parent directory; see 'go help modules
解决命令:
1 2 go env -w GO111MODULE=auto go get -u gopkg.in/ini.v1
QQ群:397745473