Go · Stage 2 – Doing Things with Values · Lesson 19 of 64

Println, Printf, and Sprintf

Choose how Go displays or creates formatted text.

Go code

fmt.Println("Score:", 10)
Line-by-line explanation
  1. Println displays its values with helpful spacing.
  2. It automatically ends the line.
Expected output
Score: 10

This is the expected result.

Printf uses placeholders such as %s for a string and %d for an integer.

Go code

fmt.Printf("%s scored %d\n", "Maya", 10)
Line-by-line explanation
  1. The format string contains two placeholders.
  2. Maya replaces %s.
  3. 10 replaces %d.
  4. \n ends the line.
Expected output
Maya scored 10

This is the expected result.

Sprintf returns formatted text instead of printing it.