Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a struct value with default zero values.
Go
type Person struct {
Name string
Age int
}
func main() {
p := [1]{}
fmt.Println(p)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'person' instead of 'Person'.
Using 'new' keyword which returns a pointer, not a value.
✗ Incorrect
To create a struct value, use the struct type name followed by curly braces.
2fill in blank
mediumComplete the code to create a struct value with field values.
Go
type Person struct {
Name string
Age int
}
func main() {
p := Person{
Name: [1],
Age: 30,
}
fmt.Println(p)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted string which causes a compile error.
Using single quotes which are for rune literals.
✗ Incorrect
String values must be enclosed in double quotes in Go.
3fill in blank
hardFix the error in creating a struct value with field names.
Go
type Person struct {
Name string
Age int
}
func main() {
p := Person{
[1]: "Bob",
Age: 25,
}
fmt.Println(p)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names which do not match struct fields.
Using all uppercase which is incorrect.
✗ Incorrect
Field names are case-sensitive and must match exactly as declared in the struct.
4fill in blank
hardFill both blanks to create a struct value with fields and print the Name.
Go
type Person struct {
Name string
Age int
}
func main() {
p := Person{
[1]: "Carol",
[2]: 40,
}
fmt.Println(p.Name)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names causing errors.
Mixing up field names.
✗ Incorrect
Use exact field names with correct capitalization to assign values.
5fill in blank
hardFill all three blanks to create a struct value, update Age, and print the updated struct.
Go
type Person struct {
Name string
Age int
}
func main() {
p := Person{
[1]: "Dave",
[2]: 28,
}
p.[3] = 29
fmt.Println(p)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names.
Forgetting to update the Age field correctly.
✗ Incorrect
Assign values using exact field names and update the Age field before printing.