0
0
Goprogramming~10 mins

Output formatting basics in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Output formatting basics
Start program
Call fmt.Printf
Process format string
Replace verbs with values
Print formatted output
End program
The program starts, calls fmt.Printf with a format string and values, processes the format string by replacing verbs with values, prints the formatted output, then ends.
Execution Sample
Go
package main
import "fmt"
func main() {
 fmt.Printf("Hello, %s! You have %d new messages.\n", "Alice", 5)
}
This Go program prints a formatted greeting with a name and number of messages.
Execution Table
StepActionFormat StringValuesOutput Produced
1Start program
2Call fmt.PrintfHello, %s! You have %d new messages.\n["Alice", 5]
3Process format stringHello, %s! You have %d new messages.\n["Alice", 5]
4Replace %s with "Alice"Hello, Alice! You have %d new messages.\n[5]Hello, Alice! You have %d new messages.\n
5Replace %d with 5Hello, Alice! You have 5 new messages.\n[]Hello, Alice! You have 5 new messages.\n
6Print formatted outputHello, Alice! You have 5 new messages.\n
7End program
💡 Program ends after printing the formatted output.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
formatStringHello, %s! You have %d new messages.\nHello, Alice! You have %d new messages.\nHello, Alice! You have 5 new messages.\n
values["Alice", 5][5][]
outputHello, Alice! You have %d new messages.\nHello, Alice! You have 5 new messages.\nHello, Alice! You have 5 new messages.\n
Key Moments - 2 Insights
Why does the output replace %s and %d with values?
Because fmt.Printf reads the format string and replaces each verb (%s, %d) with the corresponding value from the values list, as shown in steps 4 and 5 of the execution table.
What happens if the number of verbs and values don't match?
fmt.Printf will print the values for the verbs it can match and may print extra values or leave verbs unchanged if there is a mismatch, causing unexpected output. This is why matching counts is important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 5?
AHello, Alice! You have 5 new messages.\n
BHello, %s! You have %d new messages.\n
CHello, Alice! You have %d new messages.\n
DHello, %s! You have 5 new messages.\n
💡 Hint
Check the 'Output Produced' column at step 5 in the execution table.
At which step does the program actually print the final output?
AStep 3
BStep 6
CStep 4
DStep 7
💡 Hint
Look for the step labeled 'Print formatted output' in the execution table.
If we change the format string to "Hi %s!\n", how does the output change at step 6?
AHi %s!\n
BHello, Alice! You have 5 new messages.\n
CHi Alice!\n
DError because of missing %d
💡 Hint
Think about how the format string controls the printed output in the execution table.
Concept Snapshot
Go output formatting uses fmt.Printf with a format string and values.
Format verbs like %s and %d are placeholders.
Each verb is replaced by the corresponding value in order.
The final string is printed to the console.
Mismatch in verbs and values can cause unexpected output.
Full Transcript
This visual execution trace shows how Go's fmt.Printf function formats output. The program starts and calls fmt.Printf with a format string containing verbs like %s and %d, and values such as "Alice" and 5. The format string is processed step-by-step, replacing each verb with the matching value. After all replacements, the formatted string is printed to the console. The program then ends. Key points include how verbs correspond to values and the importance of matching their counts. The execution table and variable tracker show the state changes clearly at each step.