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 := trueAfter 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: GopherDefault: 5Default: 1.5Default: 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
Beginnerfmt.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: trueControlling decimal places:
fmt.Printf("Float (2 decimals): %.2f\n", height) // Float (2 decimals): 1.50Try 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:
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions