BurntSushi/toml包使用方法

toml文件

# 全局信息
title = "TOML示例"

# 应用信息
[app]
    author = "史布斯"
    organization = "Mafool"
    mark = "第一行\n第二行."            # 换行
    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"

go程序

package main

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

type Config struct {
	Title    string
	App      app
	DB       mysql `toml:"mysql"`
	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
}

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

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

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

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

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

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

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

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

type Song struct {
	Name string
	Dur  duration `toml:"duration"`
}

type duration struct {
	time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
	var err error
	d.Duration, err = time.ParseDuration(string(text))
	return err
}

func main() {
	blob := `
[[song]]
name = "天路"
duration = "4m49s"

[[song]]
name = "忘情水"
duration = "8m03s"
`
	var songs struct {
		Song []Song
	}

	// 使用 toml.Decode
	if _, err := toml.Decode(blob, &songs); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%+v\n", songs)
}

type redis struct {
	Host string
	Port int
	Auth string
}

type sentinel struct {
	Member []redis
}

func main() {
	var rds = `
[redis]
		host = "127.0.0.1"
		port = 26379

[sentinel]
	[[sentinel.member]]
		host = "127.0.0.1"
		port = 26379
		#auth = "123456"
	[[sentinel.member]]
		host = "127.0.0.1"
		port = 26380
		#auth = "123456"

[cluster]
	[[cluster.member]]
		host = "127.0.0.1"
		port = 11111
	[[cluster.member]]
		host = "127.0.0.1"
		port = 22222
`

	var config struct{
		Redis redis
		Sentinel sentinel
	}

	if _, err := toml.Decode(rds, &config); err != nil {
		panic(err)
	}
	fmt.Println(config)
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值