【GO】18.golang toml配置文件实例

  • 获取包

go get github.com/BurntSushi/toml
  • 添加.toml文件

[env]
debug       = true
host        = "127.0.0.1"
port        = 1080
ips         = ["127.0.0.1", "127.0.0.2", "127.0.0.3"]
ids         = [1, 2, 3]

[dbs.master]
host        = "127.0.0.1"
port        = 3306

[dbs.slave]
host        = "127.0.0.1"
port        = 3306

[[fruit]]
  name = "apple"

  [fruit.physical]
    color = "red"
    shape = "round"

  [[fruit.variety]]
    name = "red delicious"

  [[fruite.variety]]

  [[fruit.variety]]
    name = "granny smith"

[[fruit]]
  name = "banana"

  [[fruit.variety]]
    name = "plantain"
  • 根据toml文件创建对应go结构

package config

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

var config *Config

type Config struct {
	EnvConfig *EnvConfig           `toml:"env"`
	DBConfigs map[string]*DBConfig `toml:"dbs"`
	Fruits    []*Fruit             `toml:"fruit"`
}

func (c *Config) String() string {
	return fmt.Sprintf("%+v", *c)
}

type EnvConfig struct {
	Debug bool     `toml:"debug"`
	Host  string   `toml:"host"`
	Port  int      `toml:"port"`
	IPs   []string `toml:"ips"`
	Ids   []int    `toml:"ids"`
}

func (c *EnvConfig) String() string {
	return fmt.Sprintf("%+v", *c)
}

type DBConfig struct {
	Host string `toml:"host"`
	Port int    `toml:"port"`
}

func (c *DBConfig) String() string {
	return fmt.Sprintf("%+v", *c)
}

type Fruit struct {
	Name      string     `toml:"name"`
	Physical  *Physical  `toml:"physical"`
	Varieties []*Variety `toml:"variety"`
}

func (c *Fruit) String() string {
	return fmt.Sprintf("%+v", *c)
}

type Physical struct {
	Color string `toml:"color"`
	Shape string `toml:"shape"`
}

func (c *Physical) String() string {
	return fmt.Sprintf("%+v", *c)
}

type Variety struct {
	Name string `toml:"name"`
}

func (c *Variety) String() string {
	return fmt.Sprintf("%+v", *c)
}

func Init(file string) error {
	config = &Config{}

	_, err := toml.DecodeFile(file, config)
	if err != nil {
		return err
	}
	return nil
}

func GetConfig() *Config {
	return config
}
  • 调用

package main

import (
	"fmt"
	"gosports/gateway/config"
)

func main() {
	err := config.Init("gateway.toml")
	if err != nil{
		fmt.Printf("Init config failed! err: %s", err)
	}

	conf := config.GetConfig()
	fmt.Printf("config: %+v", conf)
}
  • 结果

config: {EnvConfig:{Debug:true Host:127.0.0.1 Port:1080 IPs:[127.0.0.1 127.0.0.2 127.0.0.3] Ids:[1 2 3]} DBConfigs:map[master:{Host:127.0.0.1 Port:3306} slave:{Host:127.0.0.1 Port:3306}] Fruits:[{Name:apple Physical:{Color:red Shape:round} Varieties:[{Name:red delicious} {Name:granny smith}]} {Name:banana Physical:<nil> Varieties:[{Name:plantain}]}]}

 

  • toml教程参考

https://tech.mojotv.cn/2018/12/26/what-is-toml

https://blog.caojun.xyz/posts/toml-lang/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值