Files
interpres/examples/basic/main.go
T

59 lines
1.0 KiB
Go
Raw Normal View History

2026-06-26 20:12:15 +02:00
// Command basic demonstrates decoding and encoding a TOML document with
// interpres.
package main
import (
"fmt"
"log"
2026-07-26 18:35:21 +02:00
"sourcedock.dev/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)
}
2026-06-26 20:12:15 +02:00
out, err := interpres.Marshal(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n--- marshal ---\n%s", out)
}