凤凰求蛊
我会使用一些流行的 toml 库将 toml 解析为结构,然后使用标准库将其编组为 JSON。下面的代码使用了https://github.com/BurntSushi/toml。请注意,为简洁起见,没有错误处理。type Config struct { Age int `json:"age,omitempty"` Cats []string `json:"cats,omitempty"` Pi float64 `json:"pi,omitempty"` Perfection []int `json:"perfection,omitempty"` DOB time.Time `json:"dob,omitempty"`}var tomlData = `Age = 25Cats = [ "Cauchy", "Plato" ]Pi = 3.14Perfection = [ 6, 28, 496, 8128 ]DOB = 1987-07-05T05:45:00Z`func main() { var conf Config toml.Decode(tomlData, &conf) j, _ := json.MarshalIndent(conf, "", " ") fmt.Println(string(j))}输出:{ "age": 25, "cats": [ "Cauchy", "Plato" ], "pi": 3.14, "perfection": [ 6, 28, 496, 8128 ], "dob": "1987-07-05T05:45:00Z"}https://play.golang.com/p/91JqFjkJIXI