I spend a lot of time automating things, both at the day job and in my personal life, and most of the time I’m writing for the command line. Even after decades of programming, there’s something about the elegance around the unix philosophy and piping that make me extremely happy.

This snippet will allow you to make your text output look much better on the screen.

documentation

# main.go
package main

import (
	"fmt"
	"os"
	"text/tabwriter"
)

func main() {
	// Observe how the b's and the d's, despite appearing in the
	// second cell of each line, belong to different columns.
	w := tabwriter.NewWriter(os.Stdout, 5, 0, 5, ' ', tabwriter.Debug)
	fmt.Fprintln(w, "id\tname\tcomplete")

	fmt.Fprintln(w, "WC-123\tcosmic signature\tfalse")
	fmt.Fprintln(w, "TX-12\tcombat site\ttrue")
	fmt.Fprintln(w, "AL-0132\tcosmic signature\tfalse")

	w.Flush()
}

# output
% go run main.go
id          |name                 |complete
WC-123      |cosmic signature     |false
TX-12       |combat site          |true
AL-0132     |cosmic signature     |false

photo: Evelyn Clement on Unsplash