Files
interpres/examples/basic/main.go
T
petrbalvin 818d863827
Release / release (push) Failing after 15s
Test / vet (push) Successful in 52s
Test / test (push) Failing after 16s
Test / toml-test (push) Successful in 44s
chore: prepare release v1.1.1
2026-07-26 18:37:36 +02:00

59 lines
1.0 KiB
Go

// Command basic demonstrates decoding and encoding a TOML document with
// interpres.
package main
import (
"fmt"
"log"
"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)
}
out, err := interpres.Marshal(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n--- marshal ---\n%s", out)
}