Challenge - 5 Problems
Struct Field Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing struct fields
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: "Alice", Age: 30} fmt.Println(p.Name, p.Age) }
Attempts:
2 left
💡 Hint
Remember how to access fields of a struct using dot notation.
✗ Incorrect
The struct fields are accessed using dot notation. fmt.Println prints the values of Name and Age separated by space.
❓ Predict Output
intermediate2:00remaining
Output when accessing nested struct fields
What will this Go program print?
Go
package main import "fmt" type Address struct { City string } type Person struct { Name string Address Address } func main() { p := Person{Name: "Bob", Address: Address{City: "Paris"}} fmt.Println(p.Address.City) }
Attempts:
2 left
💡 Hint
Access nested struct fields by chaining dot notation.
✗ Incorrect
The field City is inside Address, which is inside Person. Access it as p.Address.City.
❓ Predict Output
advanced2:00remaining
Output when accessing pointer to struct fields
What is the output of this Go program?
Go
package main import "fmt" type Car struct { Brand string } func main() { c := &Car{Brand: "Toyota"} fmt.Println(c.Brand) }
Attempts:
2 left
💡 Hint
Go lets you access struct fields through pointers directly.
✗ Incorrect
In Go, you can access fields of a struct through a pointer using dot notation without explicit dereferencing.
❓ Predict Output
advanced2:00remaining
Output when accessing uninitialized struct fields
What will this Go program print?
Go
package main import "fmt" type User struct { Username string Active bool } func main() { var u User fmt.Println(u.Username, u.Active) }
Attempts:
2 left
💡 Hint
Uninitialized struct fields have zero values.
✗ Incorrect
String zero value is empty string "" and bool zero value is false.
🧠 Conceptual
expert3:00remaining
Understanding field promotion in embedded structs
Given the following Go code, what is the output of the 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) }
Attempts:
2 left
💡 Hint
Embedded structs promote their fields to the outer struct.
✗ Incorrect
The embedded struct Animal's field Name is promoted to Dog, so d.Name accesses Animal.Name.