This project is forked from BurntSushi/toml for comment-preserving toml parser and generator, which especially suit for machine-edit config file.
This library requires Go 1.13 or newer; add it to your go.mod with:
$ go get github.com/GuanceCloud/toml
Examples
The input.toml file:
version = "v1.0.1"
release_history = [
{date = "2023-03-01", version = "v0.3", features = [
"1. [SESSION] Supports Sysmon for easy monitoring of system resources usage, such as CPU, memory, network",
"2. Start WindTerm and select the profiles directory and quit.",
"3. [TAB] Restore the last modified tab name. #626"
]},
{date = "2023-02-01", version = "v0.2"},
{date = "2023-01-01", version = "v0.1"},
]
[author]
name = "zhangsan"
age = +32
gender = "male"
"marital status" = true
hobbies = [
[
"basketball",
"football"
],
["tennis"],
[
"Snooker",
"Billy Billiards",
"Pyramid"
],
]
[[country]]
name = "China"
code = "CN"
continent = "Asia"
main_cities = [
"Beijing",
"Shanghai",
"Guangzhou"
]
[[country]]
name = "America"
code = "USA"
continent = "North America"
main_cities = [
"New York",
"Los Angeles",
"Chicago",
"San Francisco"
]
[[country.state]]
name = "Massachusetts"
area = 21000
population = 7029917
latitude = [
"41°14′ N",
"42°53′ N"
]
longitude = [
"69°56′ W",
"73°30′ W"
]
[[country.state]]
name = "Texas"
capital = "Austin"
area = 696241
population = 29145505
latitude = ["25°50′ N", "36°30′ N"]
longitude = ["93°31′ W", "106°39′ W"]
[[country]]
name = "Germany"
code = "DE"
continent = "Europe"
main_cities = [
"Hamburg",
"Munich",
"Frankfurt"
]
Which can be decoded, modified and written out with comment-preserving :
package main
import (
"log"
"os"
"github.com/GuanceCloud/toml"
)
type ReleaseHistory []struct {
Date string `toml:"date"`
Version string `toml:"version"`
Features []string `toml:"features"`
}
type Author struct {
Name string `toml:"name"`
Age int `toml:"age"`
Gender string `toml:"gender"`
MaritalStatus bool `toml:"marital status"`
Hobbies [][]string `toml:"hobbies"`
}
type Country struct {
Name string `toml:"name"`
Code string `toml:"code"`
Continent string `toml:"continent"`
MainCities []string `toml:"main_cities"`
State []State `toml:"state"`
}
type State struct {
Name string `toml:"name"`
Capital string `toml:"capital"`
Area int `toml:"area"`
Population int `toml:"population"`
Latitude [2]string `toml:"latitude"`
Longitude [2]string `toml:"longitude"`
}
type tomlStruct struct {
Version string `toml:"version"`
ReleaseHistory ReleaseHistory `toml:"release_history"`
Author Author `toml:"author"`
Country []Country `toml:"country"`
}
func main() {
var ts tomlStruct
meta, err := toml.DecodeFile("input.toml", &ts)
if err != nil {
log.Fatal(err)
}
ts.Author.Hobbies = append(ts.Author.Hobbies, []string{"volleyball"})
ts.Version = "v1.2.3"
ts.Author.Name = "GuanceCloud"
ts.Country[1].State[0].Capital = "Boston"
ts.Country[0].State = append(ts.Country[0].State, State{
Name: "Anhui",
Capital: "Hefei",
Area: 140100,
Population: 61270000,
Latitude: [2]string{"29°41′ N", "34°38′ N"},
Longitude: [2]string{"114°54′ E", "119°37′ E"},
})
enc := toml.NewEncoder(os.Stdout)
if err := enc.EncodeWithComments(ts, meta); err != nil {
log.Fatal(err)
}
}
The output will be like this:
version = "v1.0.1"
[author]
age = 32
gender = "male"
hobbies = [
[
"basketball",
"football"
],
[
"tennis"
],
[
"Snooker",
"Billy Billiards",
"Pyramid"
]
]
"marital status" = true
name = "zhangsan"
[[country]]
code = "CN"
continent = "Asia"
main_cities = [
"Beijing",
"Shanghai",
"Guangzhou"
]
name = "China"
[[country]]
code = "USA"
continent = "North America"
main_cities = [
"New York",
"Los Angeles",
"Chicago",
"San Francisco"
]
name = "America"
[[country.state]]
area = 21000
latitude = [
"41°14′ N",
"42°53′ N"
]
longitude = [
"69°56′ W",
"73°30′ W"
]
name = "Massachusetts"
population = 7029917
[[country.state]]
area = 696241
capital = "Austin"
latitude = [
"25°50′ N",
"36°30′ N"
]
longitude = [
"93°31′ W",
"106°39′ W"
]
name = "Texas"
population = 29145505
[[country]]
code = "DE"
continent = "Europe"
main_cities = [
"Hamburg",
"Munich",
"Frankfurt"
]
name = "Germany"
[[release_history]]
date = "2023-03-01"
features = [
"1. [SESSION] Supports Sysmon for easy monitoring of system resources usage, such as CPU, memory, network",
"2. Start WindTerm and select the profiles directory and quit.",
"3. [TAB] Restore the last modified tab name. #626"
]
version = "v0.3"
[[release_history]]
date = "2023-02-01"
version = "v0.2"
[[release_history]]
date = "2023-01-01"
version = "v0.1"
More complex usage
See the _example/
directory for more example.