Complete the code to access the field Name of the struct person.
package main import "fmt" type Person struct { Name string Age int } func main() { p := Person{Name: "Alice", Age: 30} fmt.Println(p.[1]) }
The field Name is accessed using p.Name. Field names are case-sensitive in Go.
Complete the code to print the Age field of the struct user.
package main import "fmt" type User struct { Username string Age int } func main() { user := User{Username: "bob123", Age: 25} fmt.Println(user.[1]) }
The Age field is accessed with user.Age. Field names must match exactly.
Fix the error in accessing the Title field of the struct book.
package main import "fmt" type Book struct { Title string Author string } func main() { book := Book{Title: "Go Programming", Author: "John"} fmt.Println(book.[1]) }
The field Title must be accessed with the exact case: book.Title. Using lowercase title causes an error.
Fill both blanks to create a map of struct fields and filter by age greater than 20.
package main import "fmt" func main() { type Employee struct { Name string Age int } employees := []Employee{{"Anna", 22}, {"Ben", 19}, {"Cara", 25}} ages := map[string]int{} for _, e := range employees { if e.Age > 20 { ages[[1]] = [2] } } fmt.Println(ages) }
The map keys are employee names (e.Name) and values are ages (e.Age) filtered by age > 20.
Fill all three blanks to create a map of uppercase names to ages for employees older than 21.
package main import ( "fmt" "strings" ) func main() { type Employee struct { Name string Age int } employees := []Employee{{"Dave", 23}, {"Eva", 20}, {"Frank", 24}} result := map[string]int{} for _, emp := range employees { if emp.[3] > 21 { result[[1]] = [2] } } fmt.Println(result) }
strings.ToUpper correctly.The map keys are uppercase employee names using strings.ToUpper(emp.Name), values are ages emp.Age, filtered by emp.Age > 21.