BASICS · IN THE APP NOW
Tabular report
Rows of a struct type, printed as an aligned table with text/tabwriter.
Offline: standard library only
What it is
tabwriter buffers tab-separated cells and pads each column to its widest cell when it is flushed. The deferred Flush is what actually writes the table.
Every template's Go program was compiled for GOOS=wasip1 GOARCH=wasm with Go 1.27, then run, vetted and tested before it was added to the app.
Files
text
report/
├── go.mod
└── main.goThe code
main.go
go
package main
import (
"fmt"
"os"
"text/tabwriter"
)
type row struct {
Package string
Lines int
Tested bool
}
func main() {
rows := []row{
{Package: "forge", Lines: 240, Tested: true},
{Package: "lab", Lines: 96, Tested: false},
}
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
defer writer.Flush()
fmt.Fprintln(writer, "PACKAGE\tLINES\tTESTED")
for _, item := range rows {
fmt.Fprintf(writer, "%s\t%d\t%t\n", item.Package, item.Lines, item.Tested)
}
}What you learn
- Define a struct type and fill a slice of it with a composite literal.
- Configure tabwriter.NewWriter with minimum width, padding and pad character.
- Separate cells with \t and rows with \n, then Flush once with defer.
- Format strings, integers and booleans with %s, %d and %t.
Run it on a server
It is an ordinary command-line program; run it anywhere Go runs.
bash
go run .Related: Types you define · Standard library patterns