github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/toml/_examples/example.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/BurntSushi/toml" 8 ) 9 10 type tomlConfig struct { 11 Title string 12 Owner ownerInfo 13 DB database `toml:"database"` 14 Servers map[string]server 15 Clients clients 16 } 17 18 type ownerInfo struct { 19 Name string 20 Org string `toml:"organization"` 21 Bio string 22 DOB time.Time 23 } 24 25 type database struct { 26 Server string 27 Ports []int 28 ConnMax int `toml:"connection_max"` 29 Enabled bool 30 } 31 32 type server struct { 33 IP string 34 DC string 35 } 36 37 type clients struct { 38 Data [][]interface{} 39 Hosts []string 40 } 41 42 func main() { 43 var config tomlConfig 44 if _, err := toml.DecodeFile("example.toml", &config); err != nil { 45 fmt.Println(err) 46 return 47 } 48 49 fmt.Printf("Title: %s\n", config.Title) 50 fmt.Printf("Owner: %s (%s, %s), Born: %s\n", 51 config.Owner.Name, config.Owner.Org, config.Owner.Bio, 52 config.Owner.DOB) 53 fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n", 54 config.DB.Server, config.DB.Ports, config.DB.ConnMax, 55 config.DB.Enabled) 56 for serverName, server := range config.Servers { 57 fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC) 58 } 59 fmt.Printf("Client data: %v\n", config.Clients.Data) 60 fmt.Printf("Client hosts: %v\n", config.Clients.Hosts) 61 }