日期:2023年3月6日
作者:Commas
签名:(ง •_•)ง 积跬步以致千里,积小流以成江海……
注释:如果您觉得有所帮助
,帮忙点个赞
,也可以关注我
,我们一起成长;如果有不对的地方,还望各位大佬不吝赐教,谢谢^ - ^
1.01365 = 37.7834;0.99365 = 0.0255
1.02365 = 1377.4083;0.98365 = 0.0006
一、前言
要在Go中读取INI文件,我们可以使用名为go-ini
的第三方库(a third-party library)。go-ini
是一个非常方便、高效的go配置文件操作库。
二、go-ini第三方库的安装
通过golang命令行下载安装即可,代码如下:
go get gopkg.in/ini.v1
或
go get github.com/go-ini/ini
另外,如果下载速度慢可尝试切换代理:
go env -w GOPROXY=https://goproxy.cn
三、go-ini读取配置文件
(3-1)准备ini文件
首先,我们准备一个ini文件,名为config,ini
,如下所示:
[server]
# debug 开发模式,release 生产模式
AppMode = debug
HttpPort = :3000
JwtKey = yourKey
[database]
Db = mysql
DbHost = 127.0.0.1
DbPort = 3306
DbUser = root
DbPassWord = yourPassword
DbName = yourDatabaseName
这里是比较常见的服务器配置文件。
- ini文件一般以分区来划分多块配置的,如上有有两个分区,分别为
[server]
和[database]
. - 每个分区下有多个
key = value
的键值对构成,如[server]
分区下的AppMode = debug
。
(3-2)加载ini文件
用法:
- 在Go代码中导入ini包;
- 使用
ini.load
函数加载ini文件,并传入ini文件的路径;
语法:
// 引入第三方包
import "gopkg.in/ini.v1"
//加载ini文件
ini.load(文件路径)
示例:
// 引入
import "gopkg.in/ini.v1"
// 加载
file, err := ini.Load("./config.ini")
// 判断
if err != nil {
fmt.Println("配置文件读取错误,请检查文件路径:", err)
}
其中,file
是一个指向配置文件结构体的指针,如下所示:
type File struct {
// Should make things safe, but sometimes doesn't matter.
BlockMode bool
NameMapper
ValueMapper
// contains filtered or unexported fields
}
(3-3)读取ini文件
要从ini文件中的指定分区(section
)和键(key
)获取值,可以使用配置对象的Section
和Key
方法。
-
Section("分区名称")
:返回分区的结构体指针;
-
Key("键名")
:是分区结构体中的一个方法,返回分区的配置项,即一个键的结构体指针。
示例:
import "gopkg.in/ini.v1"
file, err := ini.Load("./config.ini")
if err != nil {
fmt.Println("配置文件读取错误,请检查文件路径:", err)
}
// 打印
fmt.Println(file.Section("server"))
fmt.Println(file.Section("server").Key("AppMode"))
fmt.Println(file.Section("server").Key("AppMode")).String()
输出:
&{0xc000112000 server map[AppMode:0xc00010e700 HttpPort:0xc00010e770 JwtKey:0xc00010e7e0] [AppMode HttpPort JwtKey] map[AppMode:debug HttpPort::3000 JwtKey:yourKey] false }
debug
debug
(3-4)完整示例
示例:
package main
import (
"fmt"
"gopkg.in/ini.v1"
)
var (
AppMode string
HttpPort string
JwtKey string
Db string
DbHost string
DbPort string
DbUser string
DbPassWord string
DbName string
Zone int
AccessKey string
SecretKey string
Bucket string
QiniuSever string
)
// [server]
// # debug 开发模式,release 生产模式
// AppMode = debug
// HttpPort = :3000
// JwtKey = yourKey
// [database]
// Db = mysql
// DbHost = 127.0.0.1
// DbPort = 3306
// DbUser = root
// DbPassWord = yourPassword
// DbName = yourDatabaseName
func main() {
file, err := ini.Load("./config.ini")
if err != nil {
fmt.Println("配置文件读取错误,请检查文件路径:", err)
}
// server
AppMode = file.Section("server").Key("AppMode").MustString("debug")
HttpPort = file.Section("server").Key("HttpPort").MustString(":3000")
JwtKey = file.Section("server").Key("JwtKey").MustString("12js32fs72")
fmt.Printf("AppMode:%s\n", AppMode)
fmt.Printf("HttpPort:%s\n", HttpPort)
fmt.Printf("JwtKey:%s\n", JwtKey)
// database
Db = file.Section("database").Key("Db").MustString("mysql")
DbHost = file.Section("database").Key("DbHost").MustString("127.0.0.1")
DbPort = file.Section("database").Key("DbPort").MustString("3306")
DbUser = file.Section("database").Key("DbUser").MustString("root")
DbPassWord = file.Section("database").Key("DbPassWord").MustString("yourPassword")
DbName = file.Section("database").Key("DbName").MustString("yourDatabaseName")
fmt.Printf("Db:%s\n", Db)
fmt.Printf("DbHost:%s\n", DbHost)
fmt.Printf("DbPort:%s\n", DbPort)
fmt.Printf("DbUser:%s\n", DbUser)
fmt.Printf("DbPassWord:%s\n", DbPassWord)
fmt.Printf("DbName:%s\n", DbName)
}
输出:
AppMode:debug
HttpPort::3000
JwtKey:yourKey
Db:mysql
DbHost:127.0.0.1
DbPort:3306
DbUser:root
DbPassWord:yourPassword
DbName:yourDatabaseName
我的微信公众号【会飞的小猴子】,等你来关注哦 ^ - ^
参考文章:
1、《gopkg.in/ini.v1》
2、《Golang:使用ini库读取配置》
版权声明:本文为博主原创文章,如需转载,请给出:
原文链接:https://blog.csdn.net/qq_35844043/article/details/129353646