TExcelTAD
Home › Go › Go Output

Go Output

Go prints text with functions from the fmt package. There are three you will use constantly:

Function Adds spaces between arguments? Adds a newline? Formatting verbs?
fmt.Print only between two non-strings no no
fmt.Println always yes no
fmt.Printf no (you control everything) no — write \n yourself yes

fmt.Println

Println ("print line") prints its arguments separated by single spaces and ends with a newline. It is the easiest way to print values of any type.

go · try it yourself
package main

import "fmt"

func main() {
	name := "Mia"
	age := 29
	fmt.Println("Name:", name)
	fmt.Println("Age:", age, "years")
	fmt.Println() // just an empty line
	fmt.Println(3.5, true, []int{1, 2, 3})
}
Name: Mia
Age: 29 years

3.5 true [1 2 3]

fmt.Print

Print does not add a newline, and it only puts a space between two operands when neither of them is a string. That rule surprises people, so look closely at the output:

go · try it yourself
package main

import "fmt"

func main() {
	fmt.Print("Hello")
	fmt.Print("World")
	fmt.Print("\n")

	fmt.Print("a", "b", "\n") // strings: no space
	fmt.Print(1, 2, 3, "\n")  // numbers: spaces between them
	fmt.Print("x=", 10, "\n")
}
HelloWorld
ab
1 2 3
x=10

Use \n inside a string to start a new line, and \t for a tab.

fmt.Printf

Printf ("print formatted") takes a format string first. Every % sequence in it — called a verb — is replaced by the next argument. It never adds a newline by itself.

go · try it yourself
package main

import "fmt"

func main() {
	product := "Coffee"
	price := 3.5
	qty := 4

	fmt.Printf("%s costs $%.2f\n", product, price)
	fmt.Printf("%d cups = $%.2f\n", qty, price*float64(qty))
	fmt.Printf("Type of qty: %T, value: %v\n", qty, qty)
}
Coffee costs $3.50
4 cups = $14.00
Type of qty: int, value: 4

The most common verbs:

Verb Meaning
%v the value in a default format (works for anything)
%d integer in base 10
%f, %.2f floating-point number, optionally with 2 decimals
%s string
%t boolean
%T the type of the value
%% a literal percent sign

The next chapter covers verbs, widths and flags in detail.

Sprint functions: format into a string

Each print function has an S twin that returns the text as a string instead of printing it: fmt.Sprint, fmt.Sprintln and fmt.Sprintf. They are perfect for building messages.

go · try it yourself
package main

import "fmt"

func main() {
	user := "kai"
	points := 1250

	msg := fmt.Sprintf("%s has %d points", user, points)
	label := fmt.Sprint("Level ", 7)

	fmt.Println(msg)
	fmt.Println(label)
	fmt.Println(len(msg), "characters")
}
kai has 1250 points
Level 7
19 characters

Tip: There is also an F family — fmt.Fprintln(w, ...), fmt.Fprintf(w, ...) — which writes to any writer, such as a file, a network connection or os.Stderr.

Test yourself

Which function prints its arguments with spaces and a trailing newline?

  1. fmt.Print
  2. fmt.Println
  3. fmt.Printf
  4. fmt.Sprint

What does fmt.Sprintf do?

  1. Prints to standard error
  2. Returns the formatted text as a string
  3. Prints slowly
  4. Formats and prints with a newline

What does fmt.Print("a", "b") print?

  1. a b
  2. ab
  3. a\nb
  4. "a" "b"