Go Tutorial: The fmt Package
Print things properly. Format numbers, align text, build strings, and control every detail of your output.
Making your output look good
So far you have been printing things with `fmt.Println`. It works. But it is a bit like cooking a full meal and then eating it standing over the sink.
The `fmt` package can do a lot more. You can format numbers, align text into columns, control decimal places, build strings dynamically, and more.
Ten steps. By the end you will never look at `fmt.Println` the same way again.
What you'll learn in this Go the fmt package tutorial
This interactive Go tutorial has 7 hands-on exercises. Estimated time: 15 minutes.
- Print vs Println — spot the difference — `fmt.Print` stays on the same line, `fmt.Println` jumps to a new line. Run this and watch how they behave differently.
- Printf — format verbs — `fmt.Printf` uses format verbs like `%s` (string) and `%d` (int). Change the book title and page count to your own and r…
- One number, many faces 🤯 — Go has a verb for everything: `%v` (value), `%T` (type), `%b` (binary), `%x` (hex). Run this — one number, five differen…
- Width and alignment — `%10s` right-aligns in 10 spaces, `%-10s` left-aligns, `%05d` zero-pads. Run this to see columns line up.
- Decimal precision — `%.2f` gives exactly 2 decimal places. A book costs $14.99, not $14.990000. Change `%f` to `%.2f` for price and `%.1f` f…
- Sprintf — build a string, don't print it — `fmt.Sprintf` works like Printf but returns the result as a string. Build a message that says `"You returned \"1984\". I…
- Escape sequences — `\n` is a newline, `\t` is a tab, `\\` is a backslash, `\"` is a quote inside a string. Run this to see each one in acti…
Go The fmt Package concepts covered
- Making your output look good