0
0
Goprogramming~20 mins

Accessing struct fields in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Struct Field Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
A30 Alice
BPerson{Name: "Alice", Age: 30}
CName: Alice, Age: 30
DAlice 30
Attempts:
2 left
💡 Hint
Remember how to access fields of a struct using dot notation.
Predict Output
intermediate
2: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)
}
AParis
BBob
CAddress{City: "Paris"}
DCity: Paris
Attempts:
2 left
💡 Hint
Access nested struct fields by chaining dot notation.
Predict Output
advanced
2: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)
}
A&{Toyota}
BToyota
CBrand: Toyota
DCompilation error
Attempts:
2 left
💡 Hint
Go lets you access struct fields through pointers directly.
Predict Output
advanced
2: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)
}
Anil false
BCompilation error
C"" false
DUsername Active
Attempts:
2 left
💡 Hint
Uninitialized struct fields have zero values.
🧠 Conceptual
expert
3: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)
}
ABuddy
BDog{Name: "Buddy", Breed: "Beagle"}
CBeagle
DCompilation error: ambiguous field
Attempts:
2 left
💡 Hint
Embedded structs promote their fields to the outer struct.