Challenge - 5 Problems
Go Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of struct literal with field names
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) }
Attempts:
2 left
💡 Hint
Look at how fmt.Println prints structs by default.
✗ Incorrect
In Go, fmt.Println prints struct values without field names, just the values in order inside braces.
❓ Predict Output
intermediate2:00remaining
Output of struct literal without field names
What is the output of this Go program?
Go
package main import "fmt" type Point struct { X int Y int } func main() { p := Point{10, 20} fmt.Println(p) }
Attempts:
2 left
💡 Hint
Struct literals can omit field names if values are in order.
✗ Incorrect
When printing, fmt.Println shows struct fields as values in braces without field names.
❓ Predict Output
advanced2:00remaining
Output of nested struct values
What is the output of this Go program?
Go
package main import "fmt" type Address struct { City string State string } type User struct { Name string Address Address } func main() { u := User{Name: "Bob", Address: Address{City: "NY", State: "NY"}} fmt.Println(u) }
Attempts:
2 left
💡 Hint
fmt.Println prints nested structs recursively without field names.
✗ Incorrect
fmt.Println prints nested structs showing values inside braces without field names.
❓ Predict Output
advanced2:00remaining
Output of pointer to struct literal
What is the output of this Go program?
Go
package main import "fmt" type Car struct { Brand string Year int } func main() { c := &Car{Brand: "Tesla", Year: 2022} fmt.Println(*c) }
Attempts:
2 left
💡 Hint
Dereferencing pointer prints the struct value.
✗ Incorrect
Dereferencing the pointer with *c gives the struct value, printed without field names.
🧠 Conceptual
expert3:00remaining
Number of fields in struct literal with embedded struct
Given the structs below, how many fields does the struct literal have when creating a value of type Employee?
Go
type Person struct {
Name string
Age int
}
type Employee struct {
Person
ID int
}
// Creating value:
// e := Employee{Person{Name: "Eve", Age: 28}, 101}Attempts:
2 left
💡 Hint
Embedded structs count as one field in the outer struct literal.
✗ Incorrect
Employee struct has two fields: an embedded Person struct and an int ID. The literal uses two values.