有所帮助点个赞关注我



一、前言

go-inigo-ini

二、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文件

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

这里是比较常见的服务器配置文件。

[server][database]key = value[server]AppMode = debug

(3-2)加载ini文件

用法:

ini.load

语法:

// 引入第三方包
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文件

sectionkeySectionKey
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

我的微信公众号【会飞的小猴子】,等你来关注哦 ^ - ^



版权声明:本文为博主原创文章,如需转载,请给出:
原文链接:https://blog.csdn.net/qq_35844043/article/details/129353646