$ 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条)