0
0
Goprogramming~10 mins

Nested structs in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a nested struct field.

Go
type Person struct {
    Name string
    Address [1]
}

type Address struct {
    City string
    Zip  int
}
Drag options to blanks, or click blank then click option'
AAddress
Bstring
Cstruct
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using basic types like string or int instead of the struct type.
Forgetting to use the struct type name.
2fill in blank
medium

Complete the code to create a variable of nested struct type.

Go
var p [1]
p = Person{
    Name: "Alice",
    Address: Address{
        City: "Wonderland",
        Zip: 12345,
    },
}
Drag options to blanks, or click blank then click option'
AAddress
Bstring
Cstruct
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using the nested struct type Address instead of Person.
Using basic types like string.
3fill in blank
hard

Fix the error in accessing the nested struct field.

Go
fmt.Println(p.[1].City)
Drag options to blanks, or click blank then click option'
AName
BAddress
CZip
DCity
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access City directly from p.
Using the wrong field name.
4fill in blank
hard

Fill both blanks to define and initialize a nested struct inline.

Go
p := Person{
    Name: "Bob",
    Address: [1]{
        City: "[2]",
        Zip: 54321,
    },
}
Drag options to blanks, or click blank then click option'
AAddress
BPerson
CWondercity
DCityville
Attempts:
3 left
💡 Hint
Common Mistakes
Using the outer struct name instead of the nested struct type.
Putting a variable name instead of a string for the city.
5fill in blank
hard

Fill all three blanks to create a nested struct and print a nested field.

Go
type Company struct {
    Name    string
    Founder [1]
}

type Founder struct {
    FirstName string
    LastName  string
}

func main() {
    c := Company{
        Name: "TechCorp",
        Founder: [2]{
            FirstName: "Jane",
            LastName: "Doe",
        },
    }
    fmt.Println(c.Founder.[3])
}
Drag options to blanks, or click blank then click option'
AFounder
CFirstName
DLastName
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong struct type or field names.
Trying to print the whole struct instead of a field.