栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

12 配置文件ini操作

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

12 配置文件ini操作

1.安装
$ go get gopkg.in/ini.v1
2.ini内容读取
package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/ini.v1"
)



func main() {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log.Fatal("fail to read file:", err)
		os.Exit(1)
	}

	fmt.Println(cfg.SectionStrings()) //[DEFAULT mysql oracle]

	fmt.Println("mode:", cfg.Section("").Key("mode")) //mode: 1

	//mysql
	fmt.Println("address:", cfg.Section("mysql").Key("address"))   //address: 192.168.1.123
	fmt.Println("port:", cfg.Section("mysql").Key("port"))         //port: 3333
	fmt.Println("user:", cfg.Section("mysql").Key("user"))         //user: root
	fmt.Println("password:", cfg.Section("mysql").Key("password")) //password: root

	//oracle
	fmt.Println("address:", cfg.Section("oracle").Key("address"))   //address: 000.000.000.000
	fmt.Println("port:", cfg.Section("oracle").Key("port"))         //port: 9999
	fmt.Println("user:", cfg.Section("oracle").Key("user"))         //user: root
	fmt.Println("password:", cfg.Section("oracle").Key("password")) //password: root
}
3.ini配置写入
package main

import (
	"log"
	"os"

	"gopkg.in/ini.v1"
)

func main() {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log.Fatal("fail to read file:", err)
		os.Exit(1)
	}

	section, err := cfg.NewSection(ini.DefaultSection)
	section.NewKey("AppName", "test")
	section.NewKey("Mode", "1")

	section, err = cfg.NewSection("oracle")
	section.NewKey("user", "root")
	section.NewKey("password", "root")
	section.NewKey("adress", "192.168.1.123")
	section.NewKey("port", "3306")

	section, err = cfg.NewSection("mysql")
	section.NewKey("user", "root")
	section.NewKey("password", "root")
	section.NewKey("adress", "192.168.1.123")
	section.NewKey("port", "3306")

	cfg.SaveTo("config.ini")
}

输出结果(config.ini):

AppName = test
Mode    = 1

[oracle]
user     = root
password = root
adress   = 192.168.1.123
port     = 3306

[mysql]
user     = root
password = root
adress   = 192.168.1.123
port     = 3306
4.文件映射到结构体
package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/ini.v1"
)

type Config struct {
	AppName string
	Mode    int

	Mysql  MysqlConfig  `ini:"mysql"`
	Oracle OracleConfig `ini:"oracle"`
}

type MysqlConfig struct {
	User     string `ini:"user"`
	Password string `ini:"password"`
	Adress   string `ini:"adress"`
	Port     int    `ini:"port"`
}

type OracleConfig struct {
	User     string `ini:"user"`
	Password string `ini:"password"`
	Adress   string `ini:"adress"`
	Port     int    `ini:"port"`
}

func main() {
	cfg, err := ini.Load("config.ini")
	if err != nil {
		log.Fatal("fail to read file:", err)
		os.Exit(1)
	}

	config := Config{}
	cfg.MapTo(&config)

	fmt.Println(config) //{test 1 {root root 192.168.1.123 3306} {root root 192.168.1.123 3306}}
}
5.结构体映射到文件
package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/ini.v1"
)

type Config struct {
	AppName string
	Mode    int

	Mysql  MysqlConfig  `ini:"mysql"`
	Oracle OracleConfig `ini:"oracle"`
}

type MysqlConfig struct {
	User     string `ini:"user"`
	Password string `ini:"password"`
	Adress   string `ini:"adress"`
	Port     int    `ini:"port"`
}

type OracleConfig struct {
	User     string `ini:"user"`
	Password string `ini:"password"`
	Adress   string `ini:"adress"`
	Port     int    `ini:"port"`
}

func main() {
	cfg := ini.Empty()
	config := Config{
		AppName: "test",
		Mode:    1,

		Mysql: MysqlConfig{
			User:     "root",
			Password: "root",
			Adress:   "192.168.1.123",
			Port:     3306,
		},

		Oracle: OracleConfig{
			User:     "root",
			Password: "root",
			Adress:   "192.168.1.123",
			Port:     3306,
		},
	}

	err := ini.ReflectFrom(cfg, &config)
	if err != nil {
		log.Fatal("ReflectFrom failed:", err)
		os.Exit(1)
	}

	cfg.SaveTo("myconfig.ini")
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/904700.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号