Reputation: 53
I have a code, that prints values to 3 columns, but i can't print them in straight columns
fmt.Printf("%d | %.1f | %.5f | \n", int(i), x, val)
I'm getting this:
0 | 0.0 | error |
1 | 90.0 | error |
2 | 180.0 | -0.00000 |
3 | 270.0 | 3.94795 |
4 | 360.0 | error |
5 | 450.0 | error |
6 | 540.0 | -0.00000 |
7 | 630.0 | 3.94795 |
8 | 720.0 | error |
I couldn't find a way to do this in go.
Upvotes: 1
Views: 1860
Reputation: 1153
You can look at tabwriter
Package tabwriter implements a write filter (tabwriter.Writer) that translates tabbed columns in input into properly aligned text.
package main
import (
"fmt"
"os"
"text/tabwriter"
)
func main() {
w := tabwriter.NewWriter(os.Stdout, 10, 1, 1, ' ', tabwriter.Debug)
fmt.Fprintf(w, "%d\t%v\t%v\t\n", 0, 0.0, "error")
fmt.Fprintf(w, "%d\t%v\t%v\t\n", 0, 90.0, "error")
fmt.Fprintf(w, "%d\t%v\t%v\t\n", 7, 630.0, 3.94795)
w.Flush()
}
https://go.dev/play/p/_u5W46AZ5sq
Upvotes: 5