package main import ( "os" "text/tabwriter" ) func main() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug) defer w.Flush() // write some tab-separated values fmt.Fprintf(w, "Name\tPrice\tQuantity\n") fmt.Fprintf(w, "Apple\t$0.99\t10\n") fmt.Fprintf(w, "Grapes\t$1.99\t5\n") fmt.Fprintf(w, "Banana\t$0.50\t20\n") }
package main import ( "fmt" "os" "text/tabwriter" ) func main() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug) defer w.Flush() // write some tab-separated values fmt.Fprintf(w, "Name\tPrice\tQuantity\n") fmt.Fprintf(w, "Apple\t$0.99\t10\n") fmt.Fprintf(w, "Grapes\t$1.99\t5\n") fmt.Fprintf(w, "Banana\t$0.50\t20\n") // add some padding to the last column w.Flush() w.Init(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug) fmt.Fprintf(w, "\t\n") // empty row for padding fmt.Fprintf(w, "\tTotal:\t$16.90\n") // add a row for the total }This example adds some padding to the last column by printing an empty row and then adding a row for the total. Overall, the go text/tabwriter package provides an easy way to format tab-separated values into columns. It is a useful package for generating neat-looking tables for CLI applications.