Creating struct values in Go - Performance & Efficiency
Let's see how the time it takes to create struct values changes as we create more of them.
We want to know how the work grows when making many struct values.
Analyze the time complexity of the following code snippet.
package main
type Point struct {
X, Y int
}
func createPoints(n int) []Point {
points := make([]Point, n)
for i := 0; i < n; i++ {
points[i] = Point{X: i, Y: i * 2}
}
return points
}
This code creates a slice of n struct values, each with two integer fields.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that creates and assigns each struct value.
- How many times: Exactly
ntimes, once for each struct.
Each time we increase n, we create more structs one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 struct creations |
| 100 | 100 struct creations |
| 1000 | 1000 struct creations |
Pattern observation: The work grows directly with the number of structs created.
Time Complexity: O(n)
This means the time to create structs grows in a straight line as you make more of them.
[X] Wrong: "Creating structs is instant and does not depend on how many we make."
[OK] Correct: Each struct takes some time to create, so more structs mean more total time.
Understanding how creating many struct values affects time helps you reason about program speed and efficiency in real tasks.
"What if we changed the struct to include another nested struct? How would the time complexity change?"
Practice
Name as "Alice" and Age as 30 in Go?Solution
Step 1: Understand struct literal syntax
In Go, struct values are created using the syntax StructType{FieldName: value, ...} for clarity and correctness.Step 2: Match the syntax with the given options
person := Person{Name: "Alice", Age: 30} uses the correct syntax with field names and values. Options A and C use positional values without field names, which is allowed but less clear and can cause errors if order changes. person := Person{Name = "Alice", Age = 30} uses '=' which is invalid in struct literals.Final Answer:
person := Person{Name: "Alice", Age: 30} -> Option CQuick Check:
Use field names with colons in struct literals = B [OK]
- Using parentheses instead of braces
- Using '=' instead of ':' in struct literals
- Mixing field order without names causing confusion
Solution
Step 1: Recall zero value struct creation
In Go, creating a struct with zero values for all fields is done by using empty braces after the struct type: StructType{}.Step 2: Evaluate options
p := Person{} uses Person{} which creates a struct with zero values. p := Person() uses parentheses which is invalid syntax. p := new(Person) uses new(Person) which returns a pointer, not a struct value. p := Person(nil) uses nil which is invalid for struct values.Final Answer:
p := Person{} -> Option AQuick Check:
Empty braces create zero-value struct = C [OK]
- Using parentheses instead of braces
- Confusing new() pointer with struct value
- Passing nil to struct literal
type Person struct {
Name string
Age int
}
func main() {
p := Person{Name: "Bob", Age: 25}
p.Age = 26
fmt.Println(p)
}Solution
Step 1: Create struct and modify field
The struct Person is created with Name "Bob" and Age 25. Then the Age field is updated to 26.Step 2: Print the struct value
Printing the struct with fmt.Println shows the field values in braces. Since Age was updated to 26, the output is {Bob 26}.Final Answer:
{Bob 26} -> Option BQuick Check:
Updated field value shows in output = D [OK]
- Assuming original Age remains unchanged
- Expecting struct type name in print output
- Confusing syntax error with valid code
type Person struct {
Name string
Age int
}
func main() {
p := Person{Name: "Eve", Age: "30"}
fmt.Println(p)
}Solution
Step 1: Check field types in struct literal
The struct Person has Age as int type, but the code assigns "30" which is a string, causing a type mismatch error.Step 2: Identify the error cause
Assigning a string to an int field is invalid in Go and causes a compile-time error.Final Answer:
Age field has wrong type value -> Option AQuick Check:
Field types must match exactly = A [OK]
- Assigning string to int field
- Confusing struct literal syntax errors
- Thinking fmt.Println can't print structs
Solution
Step 1: Understand slice of structs syntax
A slice of structs is created using []StructType{...} with each element as a struct literal.Step 2: Check each option for correct syntax
people := []Person{{Name: "Anna", Age: 22}, {Name: "Ben", Age: 28}, {Name: "Cara", Age: 31}} correctly creates a slice with three struct literals using field names. people := [Person]{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31} uses array syntax but incorrect literal format. people := []Person{Name: "Anna", Age: 22, Name: "Ben", Age: 28, Name: "Cara", Age: 31} tries to create slice but misses braces for each struct. people := []Person{Person("Anna", 22), Person("Ben", 28), Person("Cara", 31)} uses parentheses which is invalid for struct literals.Final Answer:
people := []Person{{Name: "Anna", Age: 22}, {Name: "Ben", Age: 28}, {Name: "Cara", Age: 31}} -> Option DQuick Check:
Slice of structs uses []Type{{...}, {...}} = A [OK]
- Using parentheses instead of braces for structs
- Missing braces around each struct in slice
- Confusing array and slice syntax
Ready to prove it?
Pass the test and this pattern moves unknown
