52 lines
911 B
Go
52 lines
911 B
Go
// Command basic demonstrates decoding a TOML document with interpres.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"codeberg.org/petrbalvin/interpres"
|
|
)
|
|
|
|
const document = `
|
|
title = "interpres demo"
|
|
|
|
[server]
|
|
host = "127.0.0.1"
|
|
port = 9090
|
|
|
|
[[users]]
|
|
name = "petr"
|
|
admin = true
|
|
|
|
[[users]]
|
|
name = "guest"
|
|
admin = false
|
|
`
|
|
|
|
// Config mirrors the document above.
|
|
type Config struct {
|
|
Title string `toml:"title"`
|
|
Server struct {
|
|
Host string `toml:"host"`
|
|
Port int `toml:"port"`
|
|
} `toml:"server"`
|
|
Users []struct {
|
|
Name string `toml:"name"`
|
|
Admin bool `toml:"admin"`
|
|
} `toml:"users"`
|
|
}
|
|
|
|
func main() {
|
|
var cfg Config
|
|
if err := interpres.Unmarshal([]byte(document), &cfg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Printf("title: %s\n", cfg.Title)
|
|
fmt.Printf("server: %s:%d\n", cfg.Server.Host, cfg.Server.Port)
|
|
for _, u := range cfg.Users {
|
|
fmt.Printf("user: %-6s admin=%t\n", u.Name, u.Admin)
|
|
}
|
|
}
|