0
0
Goprogramming~20 mins

Struct usage patterns in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Struct Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of embedded struct field access
What is the output of this Go program?
Go
package main
import "fmt"
type Address struct {
    City string
}
type Person struct {
    Name string
    Address
}
func main() {
    p := Person{Name: "Alice", Address: Address{City: "Paris"}}
    fmt.Println(p.City)
}
AParis
BCompilation error: ambiguous selector
CRuntime panic
DEmpty string
Attempts:
2 left
💡 Hint
Embedded structs allow direct access to their fields.
Predict Output
intermediate
2:00remaining
Output of struct pointer modification
What will be printed by this Go program?
Go
package main
import "fmt"
type Counter struct {
    Count int
}
func (c *Counter) Increment() {
    c.Count++
}
func main() {
    c := Counter{Count: 5}
    c.Increment()
    fmt.Println(c.Count)
}
ACompilation error: cannot call pointer method on value
B5
C6
DRuntime panic
Attempts:
2 left
💡 Hint
Pointer receiver methods can be called on values; Go handles the address automatically.
🔧 Debug
advanced
2:00remaining
Identify the cause of compilation error
This Go code fails to compile. What is the cause?
Go
package main
import "fmt"
type Data struct {
    value int
}
func main() {
    d := Data{value: 10}
    fmt.Println(d.Value)
}
AMissing import for fmt package
BVariable d is not initialized
CCannot print struct field directly
DField 'Value' does not exist; Go is case-sensitive
Attempts:
2 left
💡 Hint
Check the exact spelling and capitalization of struct fields.
🧠 Conceptual
advanced
2:00remaining
Effect of struct value vs pointer receiver on method behavior
Consider a struct with two methods: one with a value receiver and one with a pointer receiver. Which statement is true about their behavior when called on a struct value?
ABoth methods modify the original struct
BThe pointer receiver method can modify the original struct; the value receiver method works on a copy
CNeither method can modify the original struct
DValue receiver methods require explicit pointer conversion to be called
Attempts:
2 left
💡 Hint
Think about how Go passes the struct to each method type.
Predict Output
expert
2:00remaining
Output of struct embedding with method overriding
What is the output of this Go program?
Go
package main
import "fmt"
type Animal struct {}
func (a Animal) Speak() string {
    return "..."
}
type Dog struct {
    Animal
}
func (d Dog) Speak() string {
    return "Woof!"
}
func main() {
    var a Animal = Dog{}
    fmt.Println(a.Speak())
}
A...
BWoof!
CCompilation error: cannot assign Dog to Animal
DRuntime panic
Attempts:
2 left
💡 Hint
Consider how method sets and interface assignments work in Go.