toml配置文件的使用方法:toml.DecodeFile

第三方库https://github.com/BurntSushi/toml

列1

1.创建结构体

package main

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"time"
)

type config struct {
     
	Title    string
	DB       mysql `toml:"mysql"`
	App      app
	Redis    map[string]redis
	Releases releases
	Company  company
}

type app struct {
     
	Author  string
	Org     string `toml:"organization"`
	Mark    string
	Release time.Time
}

type mysql struct {
     
	Server  string
	Ports   []int
	ConnMax int `toml:"connection_max"`
	Enabled bool
}

type redis struct {
     
	Host string
	Port int
}

type releases struct {
     
	Release []string
	Tags    [][]interface{
     }
}

type company struct {
     
	Name   string
	Detail detail
}

type detail struct {
     
	Type string
	Addr string
	ICP  string
}

2.配置信息:conf.toml

# 全局信息
title = "config"

# 应用信息
[app]
    author = "史布斯"
    organization = "Mafool"
    release = 2020-05-27T07:32:00Z   # 时间

# 数据库配置
[mysql]
    server = "192.168.1.1"
    ports = [ 8001, 8001, 8002 ]     # 数组
    connection_max = 5000
    enabled = true

# Redis主从
# 字典对象
[redis]
    [redis.master]
        host = "10.0.0.1"
        port = 6379
    [redis.slave]
        host = "10.0.0.1"
        port = 6380

# 二维数组
[releases]
    release = ["dev", "test", "stage", "prod"]
    tags = [["dev", "stage", "prod"],[2.2, 2.1]]


# 公司信息
#对象嵌套
[company]
    name = "xx科技"
[company.detail]
    type = "game"
    addr = "北京朝阳"
    icp = "030173"

3.测试

func main() {
     
	var conf config
	if _, err := toml.DecodeFile("./conf.toml", &conf); err != nil {
     
		panic(err)
	}

	fmt.Printf("全局信息: %+v\n\n", conf.Title)

	fmt.Printf("App信息:%+v\n\n", conf.App)

	fmt.Printf("Mysql配置:%+v\n\n", conf.DB)

	fmt.Printf("版本信息:%+v\n\n", conf.Releases)

	fmt.Printf("Redis主从:%+v\n\n", conf.Redis)

	fmt.Printf("企业信息:%+v\n\n", conf.Company)
}

4.输出

全局信息: config

App信息:{
     Author:史布斯 Org:Mafool Mark: Release:2020-05-27 07:32:00 +0000 UTC}

Mysql配置:{
     Server:192.168.1.1 Ports:[8001 8001 8002] ConnMax:5000 Enabled:true}

版本信息:{
     Release:[dev test stage prod] Tags:[[dev stage prod] [2.2 2.1]]}

Redis主从:map[master:{
     Host:10.0.0.1 Port:6379} slave:{
     Host:10.0.0.1 Port:6380}]

企业信息:{
     Name:xx科技 Detail:{
     Type:game Addr:北京朝阳 ICP:030173}}

列2

1.主结构

package main

import (
	"fmt"
	"time"

	"github.com/BurntSushi/toml"
)

//总的配置信息
type tomlConfig struct {
     
	Title   string
	Owner   owner
	DB      database `toml:"database"`
	Servers map[string]server
	Clients clients
}

//用户信息
type owner struct {
     
	Name string
	Org  string `toml:"organization"`
	Bio  string
	DOB  time.Time
}

//数据库相关信息
type database struct {
     
	Server  string
	Ports   []int
	ConnMax int `toml:"connection_max"`
	Enabled bool
}

//服务器信息
type server struct {
     
	IP string
	DC string
}

//客户端信息
type clients struct {
     
	Data  [][]interface{
     }
	Hosts []string
}

2.example.toml配置信息

# This is a TOML document. Boom.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO Likes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"

[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = ["alpha", "omega"]

3.测试

func main() {
     
	//设置结构体对象
	var config tomlConfig
	//对结构体对象填充信息,文件信息在example.toml上
	if _, err := toml.DecodeFile("./example.toml", &config); err != nil {
     
		fmt.Println(err)
		return
	}
	//输出标题,全局信息
	fmt.Printf("Title: %s\n", config.Title)
	//输出owner相关信息
	fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
		config.Owner.Name,
		config.Owner.Org,
		config.Owner.Bio,
		config.Owner.DOB)
	//输出数据库相关信息
	fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n",
		config.DB.Server,
		config.DB.Ports,
		config.DB.ConnMax,
		config.DB.Enabled)
	//输出服务器相关信息
	for serverName, server := range config.Servers {
     
		fmt.Printf("Server: %s (%s, %s)\n",
			serverName,
			server.IP,
			server.DC)
	}
	//输出客户端相关信息
	fmt.Printf("Client data: %v\n", config.Clients.Data)
	fmt.Printf("Client hosts: %v\n", config.Clients.Hosts)
}

你可能感兴趣的:(学习笔记)