教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang toml配置文件实例

golang toml配置文件实例

发布时间:2022-02-05   编辑:jiaochengji.com
教程集为您提供golang toml配置文件实例等资源,欢迎您收藏本站,我们将为您提供最新的golang toml配置文件实例资源
<ul><li> <h1>获取包</h1> </li></ul><pre><code>go get github.com/BurntSushi/toml</code></pre>

<ul><li> <h1>添加.toml文件</h1> </li></ul><pre><code>[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"</code></pre>

<ul><li> <h1>根据toml文件创建对应go结构</h1> </li></ul><pre><code>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 } </code></pre>

<ul><li> <h1>调用</h1> </li></ul><pre><code>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) } </code></pre>

<ul><li> <h1>结果</h1> </li></ul>

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}]}]}

 

<ul><li> <h1>toml教程参考</h1> </li></ul>

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

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

到此这篇关于“golang toml配置文件实例”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang toml配置文件实例
2.golang: golang读取配置文件(toml文件形式)
go读取toml文件配置文件
golang toml解析
golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)
使用docker部署一个带配置文件的golang项目
Golang配置文件解析-oozgconf
项目结构设置
golang读取toml配置文件
golang之配置文件

[关闭]
~ ~