Output formatting basics in Go - Time & Space Complexity
When we format output in Go, we often use functions that process data to create strings. Understanding how long this takes helps us write faster programs.
We want to know how the time to format output changes as the amount of data grows.
Analyze the time complexity of the following code snippet.
package main
import (
"fmt"
)
func main() {
for i := 0; i < 5; i++ {
fmt.Printf("Number: %d\n", i)
}
}
This code prints numbers from 0 to 4, formatting each number into a string before printing.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs 5 times, each time formatting and printing a number.
- How many times: 5 times, once per loop iteration.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 formatting and printing steps |
| 100 | 100 formatting and printing steps |
| 1000 | 1000 formatting and printing steps |
Pattern observation: The number of formatting operations grows directly with the number of items to print.
Time Complexity: O(n)
This means the time to format and print grows in a straight line as the number of items increases.
[X] Wrong: "Formatting output is always very fast and does not depend on how many items we print."
[OK] Correct: Each item needs its own formatting step, so more items mean more work and more time.
Knowing how output formatting scales helps you write clear and efficient code, a skill valued in many programming tasks.
"What if we changed the loop to format and print a string of length n each time? How would the time complexity change?"
Practice
%d format specifier do in fmt.Printf in Go?Solution
Step 1: Understand format specifiers in Go
The%dspecifier is used to print decimal integers in Go'sfmt.Printf.Step 2: Match specifier to data type
Since%dprints integers, it matches integer values, not floats, strings, or booleans.Final Answer:
Prints an integer value -> Option AQuick Check:
%d= integer [OK]
- Confusing %d with %f for floats
- Using %d for strings
- Thinking %d prints booleans
name using fmt.Printf in Go?Solution
Step 1: Identify the correct format specifier for strings
The format specifier for strings in Go'sfmt.Printfis%s.Step 2: Check the syntax correctness
fmt.Printf("%s", name) usesfmt.Printf("%s", name), which correctly prints the string variablename.Final Answer:
fmt.Printf("%s", name) -> Option CQuick Check:
%sprints strings [OK]
- Using %d or %f for strings
- Omitting quotes around format string
- Passing variable without format specifier
package main
import "fmt"
func main() {
x := 7
y := 3.14
fmt.Printf("x = %d, y = %.1f\n", x, y)
}Solution
Step 1: Understand the format specifiers used
%dprints integerxas 7.%.1fprints floating-pointyrounded to 1 decimal place.Step 2: Apply rounding to y
3.14 rounded to 1 decimal place is 3.1, so output is "x = 7, y = 3.1".Final Answer:
x = 7, y = 3.1 -> Option AQuick Check:
%.1f rounds float to 1 decimal [OK]
- Expecting full float without rounding
- Confusing %d with %f
- Ignoring newline character
package main
import "fmt"
func main() {
name := "Alice"
age := 30
fmt.Printf("Name: %s, Age: %f\n", name, age)
}Solution
Step 1: Check format specifiers against variable types
nameis a string, printed with%scorrectly.ageis an integer but printed with%f, which expects a float.Step 2: Identify the mismatch causing error
Using%ffor an integer causes a runtime error or incorrect output.Final Answer:
Using %f to print an integer variable -> Option DQuick Check:
Match %f with float, not int [OK]
- Using %f for integers
- Ignoring type mismatch warnings
- Assuming %f works for all numbers
fmt.Printf. Which format string correctly aligns the name left in 10 spaces and price right in 6 spaces with 2 decimals?
fmt.Printf("...", name, price)Solution
Step 1: Understand alignment flags in format strings
In Go,%-10smeans left-align string in 10 spaces;%6.2fmeans right-align float in 6 spaces with 2 decimals.Step 2: Match alignment to requirements
We want product name left-aligned in 10 spaces and price right-aligned in 6 spaces with 2 decimals, so"%-10s %6.2f\n"fits.Final Answer:
"%-10s %6.2f\n" -> Option BQuick Check:
Left-align string with %-10s, right-align float with %6.2f [OK]
- Swapping alignment flags
- Using wrong width or precision
- Forgetting to specify decimal places for floats
Ready to prove it?
Pass the test and this pattern moves unknown
