Menu
Coddy logo textTech

Format Verbs

Part of the Fundamentals section of Coddy's GO journey — lesson 30 of 109.

Let's learn about format verbs in Go. Format verbs are special placeholders that we use with fmt.Printf() to display different types of data in specific ways.

First, let's create some variables with different data types:

name := "Gopher"
age := 5
height := 1.5
isActive := true

After executing this code, we've created four variables: a string, an integer, a float, and a boolean.

Now, let's use the default format verb %v. It works with any data type and prints the value in its default format:

fmt.Printf("Default: %v\n", name)
fmt.Printf("Default: %v\n", age)
fmt.Printf("Default: %v\n", height)
fmt.Printf("Default: %v\n", isActive)

After executing this code, you will see:
Default: Gopher
Default: 5
Default: 1.5
Default: true

The %v verb automatically formats each value according to its type — no matter if it's a string, integer, float, or boolean.

Next, let's use the %d format verb specifically for integers:

fmt.Printf("Integer: %d\n", age)

After executing this code, Integer: 5 will show on the screen. The %d is perfect for displaying whole numbers.

Let's display our float value using the %f format verb:

fmt.Printf("Float: %f\n", height)

After executing this code, Float: 1.500000 will show on the screen. Notice that %f shows six decimal places by default.

We can control the number of decimal places by adding a precision specifier:

fmt.Printf("Float (2 decimals): %.2f\n", height)

After executing this code, Float (2 decimals): 1.50 will show on the screen. The .2 before f limits the output to 2 decimal places.

Finally, let's display our boolean value using the %t format verb:

fmt.Printf("Boolean: %t\n", isActive)

After executing this code, Boolean: true will show on the screen. The %t is specifically for displaying boolean values.

Remember: use %v for any type, %d for integers, %f for floats, and %t for booleans.

challenge icon

Challenge

Beginner
In this challenge, you'll practice using format verbs with fmt.Printf. You have several variables of different types already defined. Your task is to print them using the appropriate format verbs (%v, %d, %f, %s, %t, etc.).

Cheat sheet

Format verbs are special placeholders used with fmt.Printf() to display different types of data:

Common format verbs:

  • %v - default format for any type
  • %d - integers
  • %f - floats
  • %t - booleans
  • %s - strings

Basic usage:

name := "Gopher"
age := 5
height := 1.5
isActive := true

fmt.Printf("Default: %v\n", name)     // Default: Gopher
fmt.Printf("Default: %v\n", age)      // Default: 5
fmt.Printf("Default: %v\n", height)   // Default: 1.5
fmt.Printf("Default: %v\n", isActive) // Default: true
fmt.Printf("Integer: %d\n", age)      // Integer: 5
fmt.Printf("Float: %f\n", height)     // Float: 1.500000
fmt.Printf("Boolean: %t\n", isActive) // Boolean: true

Controlling decimal places:

fmt.Printf("Float (2 decimals): %.2f\n", height) // Float (2 decimals): 1.50

Try it yourself

package main

import "fmt"

func main() {
	// These variables are already defined for you
	age := 25
	height := 5.9
	isStudent := true
	name := "Alex"
	
	// TODO: Use fmt.Printf to print the following sentence with the correct format verbs:
	// "Name: Alex, Age: 25, Height: 5.900000, Student: true"
	
	// Write your code below:
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals