i want to print a newline but if i add a newline it changes the format, here is the code.
q := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.AlignRight|tabwriter.Debug)
fmt.Fprintf(q, "Replica\tStatus\tDataUpdateIndex\t
")
fmt.Fprintf(q, "
")
for i := 0; i < 4; i += 3 {
fmt.Fprintf(q, "%s\t%s\t%s\t
", statusArray[i], statusArray[i+1], statusArray[i+2])
}
How to add newline without affecting the format?
As stated in the docs (emphasis is mine):
Tab-terminated cells in contiguous lines constitute a column.
https://golang.org/pkg/text/tabwriter/#Writer
When you insert the new line in your code, you are separating the header and content lines so they are not treated as "columns".
In order to fix that, make your newline insert an empty row with the same number of columns (but blank).
fmt.Fprintf(q, "Replica\tStatus\tDataUpdateIndex\t
")
fmt.Fprintf(q, "\t\t\t
") // blank line
for i := 0; i < 4; i += 3 {
fmt.Fprintf(q, "%s\t%s\t%s\t
", statusArray[i], statusArray[i+1], statusArray[i+2])
}