Challenge - 5 Problems
Master of Passing Values and Pointers in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of modifying a struct passed by value
What is the output of this Go program?
Go
package main import "fmt" type Point struct { X, Y int } func move(p Point) { p.X = 10 p.Y = 20 } func main() { pt := Point{1, 2} move(pt) fmt.Println(pt) }
Attempts:
2 left
💡 Hint
Think about whether the function changes the original struct or a copy.
✗ Incorrect
The function move receives a copy of the struct Point. Changes inside move do not affect the original variable pt in main. So pt remains {1 2}.
❓ Predict Output
intermediate2:00remaining
Output of modifying a struct passed by pointer
What is the output of this Go program?
Go
package main import "fmt" type Point struct { X, Y int } func move(p *Point) { p.X = 10 p.Y = 20 } func main() { pt := Point{1, 2} move(&pt) fmt.Println(pt) }
Attempts:
2 left
💡 Hint
Check if the function receives a pointer and modifies the original struct.
✗ Incorrect
The function move receives a pointer to Point. It modifies the original struct fields through the pointer. So pt changes to {10 20}.
❓ Predict Output
advanced2:00remaining
Effect of pointer vs value on slice modification
What is the output of this Go program?
Go
package main import "fmt" func modifySlice(s []int) { s[0] = 100 s = append(s, 200) } func main() { slice := []int{1, 2, 3} modifySlice(slice) fmt.Println(slice) }
Attempts:
2 left
💡 Hint
Remember slices are references but append may create a new underlying array.
✗ Incorrect
The function modifies the first element of the original slice, so s[0] becomes 100. But append creates a new slice inside the function, which does not affect the original slice length. So original slice remains length 3.
❓ Predict Output
advanced2:00remaining
Output when passing map by value and modifying it
What is the output of this Go program?
Go
package main import "fmt" func modifyMap(m map[string]int) { m["a"] = 100 m = make(map[string]int) m["b"] = 200 } func main() { myMap := map[string]int{"a": 1} modifyMap(myMap) fmt.Println(myMap) }
Attempts:
2 left
💡 Hint
Maps are reference types in Go. Changes to the map inside the function affect the original map.
✗ Incorrect
The function modifies the original map by setting key "a" to 100. Then it assigns a new map to the local variable m, but this does not affect the original map outside. So the original map has key "a" with value 100.
🧠 Conceptual
expert3:00remaining
Understanding pointer semantics with interface values
Consider this Go code snippet. What will be printed?
package main
import "fmt"
type Data struct {
Value int
}
func modify(d interface{}) {
switch v := d.(type) {
case *Data:
v.Value = 42
case Data:
v.Value = 42
}
}
func main() {
d1 := Data{1}
d2 := &Data{1}
modify(d1)
modify(d2)
fmt.Println(d1.Value, d2.Value)
}
Attempts:
2 left
💡 Hint
Think about whether the modify function changes the original data or a copy depending on the type assertion.
✗ Incorrect
When modify receives d1 (a value), the case Data matches and modifies a copy, so d1.Value stays 1. When modify receives d2 (a pointer), the case *Data matches and modifies the original, so d2.Value becomes 42.