Type inference in Go - Time & Space Complexity
Type inference helps the computer guess variable types automatically. We want to see how this guessing affects the program's speed.
Does type inference change how long the program takes to run?
Analyze the time complexity of the following code snippet.
package main
func sum(numbers []int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
func main() {
nums := []int{1, 2, 3, 4, 5}
_ = sum(nums)
}
This code sums all numbers in a list using type inference for variables.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list.
- How many times: Once for every number in the list.
As the list gets bigger, the program checks more numbers one by one.
| 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 with the list size.
[X] Wrong: "Type inference makes the program slower because it guesses types at runtime."
[OK] Correct: Type inference happens when the program is compiled, so it does not slow down the running program.
Understanding how type inference works helps you explain code clarity and performance. It shows you know how the language handles types behind the scenes.
"What if we changed the loop to a recursive function? How would the time complexity change?"
Practice
x := 10 inside a function?Solution
Step 1: Understand the := operator in Go
The := operator declares a variable and assigns a value, letting Go guess the type.Step 2: Check the assigned value type
Since 10 is an integer, Go infers the type ofxas int automatically.Final Answer:
Go automatically figures out thatxis an int. -> Option BQuick Check:
Type inference means Go guesses type from value [OK]
- Thinking you must always write the type explicitly
- Assuming variables have no type
- Believing Go defaults to string type
Solution
Step 1: Recall Go syntax for type inference
Inside functions,:=declares and assigns a variable with inferred type.Step 2: Check each option
var x int = 5 declares with explicit type, not inference. x = 5 misses declaration. int x = 5 is invalid Go syntax.Final Answer:
x := 5 -> Option AQuick Check:
Use := to declare and infer type [OK]
- Using C or D which are invalid Go syntax
- Forgetting to declare variable before assignment
- Confusing var declaration with :=
package main
import "fmt"
func main() {
x := 3.14
fmt.Printf("%T", x)
}Solution
Step 1: Identify the type inferred for 3.14
In Go, untyped floating-point constants default to float64 when assigned with :=.Step 2: Understand fmt.Printf with %T
The %T verb prints the type of the variable, so it will print "float64".Final Answer:
float64 -> Option AQuick Check:
Float literals default to float64 [OK]
- Assuming float32 is default
- Thinking 3.14 is int
- Confusing %T output with value
func main() {
var x := 10
fmt.Println(x)
}Solution
Step 1: Understand variable declaration syntax
In Go, := is shorthand for declaration and assignment without var keyword.Step 2: Identify the syntax error
Using var and := together is invalid syntax; you must use either var x = 10 or x := 10.Final Answer:
You cannot use := with var keyword. -> Option CQuick Check:
var and := cannot be combined [OK]
- Mixing var and := together
- Ignoring import errors (not shown here)
- Thinking := works outside functions
result after execution?
func main() {
a := 5
b := 2.0
result := a * int(b)
fmt.Printf("%T", result)
}Solution
Step 1: Analyze variable types and conversion
a is int, b is float64 (default for 2.0). The expression converts b to int before multiplication.Step 2: Determine result type
Multiplying int (a) by int (converted b) results in int type for result.Final Answer:
int -> Option DQuick Check:
int * int = int type [OK]
- Assuming result is float64 without conversion
- Forgetting to convert float to int
- Confusing multiplication type rules
Ready to prove it?
Pass the test and this pattern moves unknown
