阿波罗的战车
2019 年的一个小更新 - 现在有更新的BurntSushi/toml替代品,具有更丰富的 API 来处理.toml文件:颗粒/go-toml(和文档)例如有config.toml文件(或在内存中):[postgres]user = "pelletier"password = "mypassword"除了使用颗粒/go-toml将整个事物常规编组和解组为预定义结构(您可以在接受的答案中看到)之外,您还可以查询这样的单个值:config, err := toml.LoadFile("config.toml")if err != nil { fmt.Println("Error ", err.Error())} else { // retrieve data directly directUser := config.Get("postgres.user").(string) directPassword := config.Get("postgres.password").(string) fmt.Println("User is", directUser, " and password is", directPassword) // or using an intermediate object configTree := config.Get("postgres").(*toml.Tree) user := configTree.Get("user").(string) password := configTree.Get("password").(string) fmt.Println("User is", user, " and password is", password) // show where elements are in the file fmt.Printf("User position: %v\n", configTree.GetPosition("user")) fmt.Printf("Password position: %v\n", configTree.GetPosition("password")) // use a query to gather elements without walking the tree q, _ := query.Compile("$..[user,password]") results := q.Execute(config) for ii, item := range results.Values() { fmt.Println("Query result %d: %v", ii, item) }}