Struct pointers let you work with the address of a struct instead of copying it. This helps save memory and lets you change the original struct easily.
Struct pointers in Go
Introduction
Syntax
Go
type StructName struct {
Field1 type1
Field2 type2
}
var ptr *StructName = &StructName{Field1: value1, Field2: value2}The *StructName means a pointer to a struct of type StructName.
The & operator gets the address of a struct value.
Examples
Person, then make a pointer pPtr to it.Go
type Person struct {
Name string
Age int
}
var p = Person{Name: "Alice", Age: 30}
var pPtr *Person = &pAge field through the pointer. This updates the original struct.Go
pPtr.Age = 31Person and updates the age directly.Go
func updateAge(p *Person, newAge int) {
p.Age = newAge
}Sample Program
This program shows how to use a struct pointer to update the original struct inside a function.
Go
package main import "fmt" type Person struct { Name string Age int } func updateAge(p *Person, newAge int) { p.Age = newAge } func main() { p := Person{Name: "Bob", Age: 25} fmt.Println("Before update:", p) updateAge(&p, 26) fmt.Println("After update:", p) }
Important Notes
Using pointers avoids copying the whole struct, which is faster for big structs.
Access struct fields through pointers using the dot . operator directly; Go handles the pointer dereference automatically.
Summary
Struct pointers hold the address of a struct, not a copy.
They let you change the original struct from functions or other parts of your code.
Use & to get a pointer and pass it to functions for efficient updates.
Practice
1. What does a struct pointer in Go hold?
type Person struct { Name string }easy
Solution
Step 1: Understand what a pointer stores
A pointer stores the memory address of a variable, not the variable's value itself.Step 2: Apply to struct pointers
A struct pointer holds the address of the struct instance, allowing access or modification of the original struct.Final Answer:
The memory address of a struct instance -> Option CQuick Check:
Struct pointer = memory address [OK]
Hint: Pointers hold addresses, not copies. Remember & for address [OK]
Common Mistakes:
- Thinking pointer holds a copy of the struct
- Confusing pointer with struct value
- Assuming pointer holds only first field
2. Which of the following is the correct way to declare a pointer to a struct
Person in Go?easy
Solution
Step 1: Recall Go pointer syntax
In Go, a pointer type is declared by placing * before the type name, e.g., *Person.Step 2: Match correct declaration
var p *Person usesvar p *Person, which is the correct syntax for a pointer variable.Final Answer:
var p *Person -> Option DQuick Check:
Pointer declaration = *Type [OK]
Hint: Pointer type syntax: *TypeName, not after variable [OK]
Common Mistakes:
- Placing * after variable name
- Using & in declaration instead of *
- Incorrect order of * and type
3. What is the output of this Go program?
package main
import "fmt"
type Point struct { X, Y int }
func main() {
p := Point{1, 2}
ptr := &p
ptr.X = 10
fmt.Println(p.X)
}medium
Solution
Step 1: Understand pointer modification
ptr points to p. Changing ptr.X modifies p.X because ptr references p's memory.Step 2: Check printed value
After setting ptr.X = 10, p.X becomes 10. So, printing p.X outputs 10.Final Answer:
10 -> Option BQuick Check:
Pointer changes original struct field [OK]
Hint: Changing pointer field changes original struct field [OK]
Common Mistakes:
- Thinking ptr.X is separate copy
- Expecting original p.X to remain 1
- Confusing pointer dereference syntax
4. Identify the error in this Go code snippet:
package main
import "fmt"
type User struct { Name string }
func updateName(u User) {
u.Name = "Alice"
}
func main() {
user := User{"Bob"}
updateName(&user)
fmt.Println(user.Name)
}medium
Solution
Step 1: Check function parameter type
Function updateName expects a User value, but main passes &user (a *User pointer).Step 2: Identify the type mismatch
This causes a compilation error because the argument type (*User) does not match the parameter type (User).Final Answer:
Passing pointer to function expecting value -> Option AQuick Check:
Function param must match argument type [OK]
Hint: Match function param type with argument (& for pointer) [OK]
Common Mistakes:
- Passing pointer to value parameter without pointer receiver
- Ignoring need to modify original struct
- Assuming string assignment causes error
5. Given this struct and function:
What will happen when running this program?
package main
import "fmt"
type Counter struct { Count int }
func increment(c *Counter) {
c.Count++
}
func main() {
c := Counter{Count: 5}
increment(&c)
increment(c)
fmt.Println(c.Count)
}What will happen when running this program?
hard
Solution
Step 1: Analyze function calls
increment expects a *Counter pointer. The first call increment(&c) is correct.Step 2: Check second call increment(c)
Passing c (a value) instead of *Counter causes a compilation error because types don't match.Final Answer:
Compilation error at increment(c) -> Option AQuick Check:
Pointer param requires pointer argument [OK]
Hint: Pointer params need pointer args; value won't auto-convert [OK]
Common Mistakes:
- Assuming Go auto-converts value to pointer
- Expecting output 7 without error
- Ignoring type mismatch in function call
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
