Challenge - 5 Problems
Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:00remaining
Purpose of structs in Go
Why do Go programmers use structs in their programs?
Attempts:
2 left
💡 Hint
Think about how you can organize information about a person or an object.
✗ Incorrect
Structs let you combine different pieces of data, like a name and age, into one object. This helps keep related information together.
❓ Predict Output
intermediate1:30remaining
Output of struct field access
What is the output of this Go program?
Go
package main import "fmt" type Person struct { Name string Age int } func main() { p := Person{Name: "Anna", Age: 30} fmt.Println(p.Name) }
Attempts:
2 left
💡 Hint
Look at which field is printed with fmt.Println.
✗ Incorrect
The program prints the Name field of the struct, which is "Anna".
❓ Predict Output
advanced1:30remaining
Structs and pointers output
What will this Go program print?
Go
package main import "fmt" type Point struct { X, Y int } func main() { p := Point{X: 5, Y: 10} q := &p q.X = 20 fmt.Println(p.X, p.Y) }
Attempts:
2 left
💡 Hint
Changing a field through a pointer affects the original struct.
✗ Incorrect
q points to p, so changing q.X changes p.X. The Y field stays the same.
🧠 Conceptual
advanced1:00remaining
Why use structs instead of separate variables?
What is a key advantage of using structs over separate variables for related data?
Attempts:
2 left
💡 Hint
Think about organizing information logically.
✗ Incorrect
Structs group related data, so you can handle it as one object, improving clarity and maintenance.
❓ Predict Output
expert2:00remaining
Struct embedding and field access output
What is the output of this Go program?
Go
package main import "fmt" type Animal struct { Name string } type Dog struct { Animal Breed string } func main() { d := Dog{Animal: Animal{Name: "Buddy"}, Breed: "Beagle"} fmt.Println(d.Name, d.Breed) }
Attempts:
2 left
💡 Hint
Embedded structs let you access fields directly.
✗ Incorrect
Dog embeds Animal, so d.Name accesses Animal's Name field directly.