Basic data types in Go - Time & Space Complexity
When working with basic data types in Go, it's helpful to know how fast operations run.
We want to see how the time to do simple tasks changes as the input size grows.
Analyze the time complexity of the following code snippet.
package main
func sumInts(numbers []int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
This code adds up all numbers in a list of integers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each number to total inside a loop.
- How many times: Once for each number in the list.
As the list gets longer, the time to add all numbers grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input list gets bigger.
[X] Wrong: "Adding numbers is always instant, no matter how many there are."
[OK] Correct: Even simple addition takes time, and doing it many times adds up as the list grows.
Understanding how basic operations scale helps you explain code efficiency clearly and confidently.
"What if we changed the list to a fixed size array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the data types
In Go,boolis used for true/false values,intfor whole numbers,stringfor text, andfloat64for decimals.Step 2: Match the type to true/false
Since true/false are boolean values, the correct type isbool.Final Answer:
bool -> Option DQuick Check:
Boolean type = bool [OK]
- Choosing int for true/false
- Using string for boolean values
- Confusing float64 with bool
age in Go?Solution
Step 1: Recall Go variable declaration syntax
In Go, variables are declared withvarfollowed by the name and then the type.Step 2: Check each option
var age int matches this syntax:var age int. Others do not follow Go syntax.Final Answer:
var age int -> Option BQuick Check:
Variable declaration = var name type [OK]
- Writing type before variable name
- Using := without initialization
- Mixing order of var and type
package main
import "fmt"
var x int = 10
var y float64 = 5.5
var z string = "Go"
func main() {
fmt.Println(x, y, z)
}Solution
Step 1: Understand variable values
Variablexis 10 (int),yis 5.5 (float64), andzis "Go" (string).Step 2: Check fmt.Println output
fmt.Printlnprints values separated by spaces without quotes for strings.Final Answer:
10 5.5 Go -> Option AQuick Check:
Print values as is, strings without quotes [OK]
- Expecting quotes around strings
- Confusing float64 with int
- Thinking fmt.Println causes error
var name string = 123 fmt.Println(name)
Solution
Step 1: Check variable assignment types
The variablenameis declared asstringbut assigned the integer 123, which is a type mismatch.Step 2: Verify other syntax
Semicolons are optional in Go,fmt.Printlnis correctly used, andnameis not a reserved keyword.Final Answer:
Cannot assign int to string variable -> Option CQuick Check:
Type mismatch error = assigning int to string [OK]
- Assigning wrong type value
- Thinking semicolon is mandatory
- Confusing reserved keywords
Solution
Step 1: Match data types to real-world data
Age is a whole number, soint. Height can have decimals, sofloat64. Name is text, sostring. Student status is true/false, sobool.Step 2: Check each option
var age int var height float64 var name string var isStudent bool matches these types correctly. Others assign wrong types to variables.Final Answer:
var age int var height float64 var name string var isStudent bool -> Option AQuick Check:
Correct types for data = int, float64, string, bool [OK]
- Mixing int and float64
- Using bool for text
- Assigning string to boolean
Ready to prove it?
Pass the test and this pattern moves unknown
