go read toml file configuration file

Copyright: by DongBao https://blog.csdn.net/aaaadong/article/details/91386088

1. Prepare a reference project

go get github.com/BurntSushi/toml

2. Code

package main

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

func main() {
	fpath := "/Users/dong/Desktop/config.toml"
	var conf Config
	if _, err := toml.DecodeFile(fpath, &conf); err != nil {
		fmt.Println("decode config file err")
	}
	fmt.Println(conf)
}

type Config struct {
	Common Common
}

type Common struct {
	Base string
	Addr string
}

3.toml file config.toml

# This is a TOML document.

# dev(debug) config.

[common]
base = "http://localhost"
addr = ":5000"

4. Read results

Guess you like

Origin blog.csdn.net/aaaadong/article/details/91386088