Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The field Address inside Person must be of type Address to nest the struct.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the nested struct type
Address instead of Person.Using basic types like string.
✗ Incorrect
The variable p must be of type Person to hold the nested Address struct.
3fill in blank
hardFix the error in accessing the nested struct field.
Go
fmt.Println(p.[1].City) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access
City directly from p.Using the wrong field name.
✗ Incorrect
To access the city, you must first access the Address field inside p.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The nested struct type is Address, and the city name is a string like Cityville.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong struct type or field names.
Trying to print the whole struct instead of a field.
✗ Incorrect
The Founder field in Company is of type Founder. We initialize it with Founder{...} and print the FirstName field.