example.go 1.2 KB

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