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 10 hands-on exercises. Estimated time: 15 minutes.
- Print vs Println — spot the difference — `fmt.Println` prints your text and then moves to the next line.
- Printf — your first format verb — `fmt.Printf` lets you build output by mixing text with variables using format verbs.
- More format verbs — Go has a format verb for almost everything.
- Width and alignment — You can tell Go exactly how wide a value should be when printed.
- Decimal precision — By default `%f` prints 6 decimal places, which is usually too many.
- Sprintf — build a string, don't print it — `fmt.Sprintf` works exactly like `Printf` but instead of printing, it returns the result as a string.
- Escape sequences — Some characters cannot just be typed into a string. They need a special code called an escape sequence.
- Println with multiple arguments — You already know `fmt.Println(oneValue)` — but it can take multiple values at once.
- Printf with multiple verbs — A single `Printf` can hold as many format verbs as you need.
- Build a formatted profile card — No starter code.
Go The fmt Package concepts covered
- Making your output look good