import ( "bufio" "os" "text/tabwriter" ) func main() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) // Initialize writer with fixed column width of 10 w.Init(os.Stdout, 10, 0, 1, ' ', tabwriter.TabIndent) // Write data to the writer fmt.Fprintf(w, "Column1\tColumn2\tColumn3\n") fmt.Fprintf(w, "Data1\tData2\tData3\n") fmt.Fprintf(w, "MoreData1\tMoreData2\tMoreData3\n") // Flush the writer to output w.Flush() }
import ( "bufio" "os" "text/tabwriter" ) func main() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) // Get the maximum width of each column from the input data col1Width, col2Width, col3Width := getMaxColumnWidths(inputData) // Initialize writer with dynamic column widths w.Init(os.Stdout, col1Width+1, 0, 1, ' ', tabwriter.TabIndent) // Write data to the writer fmt.Fprintf(w, "Column1\tColumn2\tColumn3\n") for _, row := range inputData { fmt.Fprintf(w, "%s\t%s\t%s\n", row[0], row[1], row[2]) } // Flush the writer to output w.Flush() }These examples demonstrate the use of the `Init` method of the `tabwriter.Writer` type to initialize a tab-separated table with fixed or dynamic column widths. The `text/tabwriter` package is part of the Go standard library.