Bird
Raised Fist0
Goprogramming~10 mins

Accessing struct fields in Go - Interactive Code Practice

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

Complete the code to access the field Name of the struct person.

Go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p.[1])
}
Drag options to blanks, or click blank then click option'
AAge
BName
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names which do not exist.
Trying to access fields without the dot operator.
2fill in blank
medium

Complete the code to print the Age field of the struct user.

Go
package main

import "fmt"

type User struct {
    Username string
    Age      int
}

func main() {
    user := User{Username: "bob123", Age: 25}
    fmt.Println(user.[1])
}
Drag options to blanks, or click blank then click option'
AAge
BUsername
Cage
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names that do not exist.
Trying to access fields without the dot operator.
3fill in blank
hard

Fix the error in accessing the Title field of the struct book.

Go
package main

import "fmt"

type Book struct {
    Title  string
    Author string
}

func main() {
    book := Book{Title: "Go Programming", Author: "John"}
    fmt.Println(book.[1])
}
Drag options to blanks, or click blank then click option'
Atitle
BAuthor
Cauthor
DTitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase field names that do not exist.
Confusing field names with variable names.
4fill in blank
hard

Fill both blanks to create a map of struct fields and filter by age greater than 20.

Go
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)
}
Drag options to blanks, or click blank then click option'
Ae.Name
Be.Age
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values in the map.
Using incorrect field names.
5fill in blank
hard

Fill all three blanks to create a map of uppercase names to ages for employees older than 21.

Go
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)
}
Drag options to blanks, or click blank then click option'
Astrings.ToUpper(emp.Name)
Bemp.Age
CAge
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using field names without the struct variable prefix.
Not calling strings.ToUpper correctly.

Practice

(1/5)
1.

Which of the following is the correct way to access the Age field of a struct variable person in Go?

easy
A. person.Age()
B. person->Age
C. person["Age"]
D. person.Age

Solution

  1. Step 1: Understand struct field access syntax in Go

    In Go, struct fields are accessed using dot notation: structVariable.fieldName.
  2. Step 2: Apply to the given variable and field

    For variable person and field Age, the correct access is person.Age.
  3. Final Answer:

    person.Age -> Option D
  4. Quick Check:

    Use dot notation for struct fields = person.Age [OK]
Hint: Use dot (.) to access struct fields in Go [OK]
Common Mistakes:
  • Using arrow (->) like in C/C++
  • Trying to access fields like map keys
  • Calling field as a function with ()
2.

Which of the following code snippets correctly accesses the Title field of a struct book?

type Book struct {
    Title string
    Author string
}

var book Book
// Access Title here
easy
A. book.Title
B. book->Title
C. book["Title"]
D. book.Title()

Solution

  1. Step 1: Recall Go struct field access syntax

    Fields in Go structs are accessed using dot notation without parentheses or brackets.
  2. Step 2: Apply to the book variable and Title field

    The correct syntax is book.Title.
  3. Final Answer:

    book.Title -> Option A
  4. Quick Check:

    Dot notation accesses struct fields = book.Title [OK]
Hint: Remember: struct fields use dot, not brackets or arrows [OK]
Common Mistakes:
  • Using brackets like a map
  • Using arrow operator (->)
  • Calling field as a method
3.

What will be the output of the following Go program?

package main
import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p.Age)
}
medium
A. Alice
B. 30
C. Name
D. Compilation error

Solution

  1. Step 1: Understand struct initialization and field access

    The struct Person is initialized with Name: "Alice" and Age: 30. The variable p holds this struct.
  2. Step 2: Identify the printed field

    The code prints p.Age, which is 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Print struct field Age = 30 [OK]
Hint: Print struct.field to get that field's value [OK]
Common Mistakes:
  • Printing the Name field instead of Age
  • Expecting field name as output
  • Thinking code causes error
4.

Identify the error in the following Go code and choose the correct fix:

package main
import "fmt"

type Car struct {
    Model string
    Year  int
}

func main() {
    c := Car{Model: "Tesla", Year: 2022}
    fmt.Println(c->Model)
}
medium
A. Add parentheses: c.Model()
B. Change c->Model to c["Model"]
C. Change c->Model to c.Model
D. No error, code is correct

Solution

  1. Step 1: Identify incorrect field access operator

    The code uses c->Model, which is invalid in Go. Go uses dot notation for struct fields.
  2. Step 2: Correct the field access syntax

    Replace c->Model with c.Model to fix the error.
  3. Final Answer:

    Change c->Model to c.Model -> Option C
  4. Quick Check:

    Use dot (.) not arrow (->) for struct fields [OK]
Hint: Use dot (.) for struct fields, not arrow (->) [OK]
Common Mistakes:
  • Using arrow operator like in C/C++
  • Trying to access fields like map keys
  • Calling field as a function
5.

Given the struct and code below, what will be the output?

package main
import "fmt"

type Employee struct {
    ID    int
    Name  string
    Title string
}

func main() {
    e := Employee{ID: 101, Name: "Bob", Title: "Developer"}
    e.Title = "Senior Developer"
    fmt.Println(e.Title)
}
hard
A. Senior Developer
B. Developer
C. Title
D. Compilation error

Solution

  1. Step 1: Understand struct field update

    The struct Employee is created with Title: "Developer". Then the field Title is updated to "Senior Developer".
  2. Step 2: Identify printed value

    The code prints e.Title after update, so output is Senior Developer.
  3. Final Answer:

    Senior Developer -> Option A
  4. Quick Check:

    Updated struct field prints new value [OK]
Hint: Assign new value with dot, then print updated field [OK]
Common Mistakes:
  • Expecting original value instead of updated
  • Thinking field cannot be changed
  • Confusing field name with value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard