12 配置文件ini操作

1.安装

$ go get gopkg.in/ini.v1

2.ini内容读取

package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/ini.v1"
)

/* config.ini
mode=1

[mysql]
address=192.168.1.123
port=3333
user=root
password=root

[oracle]
address=000.000.000.000
port=9999
user=root
password=root
*/

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")
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值