Structs group related data together. Accessing struct fields lets you get or change specific pieces of that data.
Accessing struct fields in Go
structVariable.fieldName
Use the dot . to get or set a field inside a struct variable.
Field names start with a capital letter if you want to access them from other packages.
type Person struct {
Name string
Age int
}
var p Person
p.Name = "Alice"
p.Age = 30
fmt.Println(p.Name, p.Age)p := Person{Name: "Bob", Age: 25}
fmt.Println(p.Name)
fmt.Println(p.Age)type Point struct {
X, Y int
}
pt := Point{X: 10, Y: 20}
pt.X = 15
fmt.Println(pt.X, pt.Y)This program creates a Car struct, prints its fields, updates the Year field, and prints it again.
package main import "fmt" type Car struct { Brand string Year int } func main() { myCar := Car{Brand: "Toyota", Year: 2020} fmt.Println("Brand:", myCar.Brand) fmt.Println("Year:", myCar.Year) // Update the year myCar.Year = 2022 fmt.Println("Updated Year:", myCar.Year) }
If you have a pointer to a struct, you can access fields with the same dot syntax, Go handles the pointer automatically.
Trying to access a field that does not exist will cause a compile error.
Use structVariable.fieldName to access or change a struct's field.
Fields hold the data inside a struct and can be read or updated.
Structs help organize related data in one place.
Practice
Which of the following is the correct way to access the Age field of a struct variable person in Go?
Solution
Step 1: Understand struct field access syntax in Go
In Go, struct fields are accessed using dot notation:structVariable.fieldName.Step 2: Apply to the given variable and field
For variablepersonand fieldAge, the correct access isperson.Age.Final Answer:
person.Age -> Option DQuick Check:
Use dot notation for struct fields = person.Age [OK]
- Using arrow (->) like in C/C++
- Trying to access fields like map keys
- Calling field as a function with ()
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 hereSolution
Step 1: Recall Go struct field access syntax
Fields in Go structs are accessed using dot notation without parentheses or brackets.Step 2: Apply to the
The correct syntax isbookvariable andTitlefieldbook.Title.Final Answer:
book.Title -> Option AQuick Check:
Dot notation accesses struct fields = book.Title [OK]
- Using brackets like a map
- Using arrow operator (->)
- Calling field as a method
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)
}Solution
Step 1: Understand struct initialization and field access
The structPersonis initialized withName: "Alice"andAge: 30. The variablepholds this struct.Step 2: Identify the printed field
The code printsp.Age, which is 30.Final Answer:
30 -> Option BQuick Check:
Print struct field Age = 30 [OK]
- Printing the Name field instead of Age
- Expecting field name as output
- Thinking code causes error
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)
}Solution
Step 1: Identify incorrect field access operator
The code usesc->Model, which is invalid in Go. Go uses dot notation for struct fields.Step 2: Correct the field access syntax
Replacec->Modelwithc.Modelto fix the error.Final Answer:
Change c->Model to c.Model -> Option CQuick Check:
Use dot (.) not arrow (->) for struct fields [OK]
- Using arrow operator like in C/C++
- Trying to access fields like map keys
- Calling field as a function
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)
}Solution
Step 1: Understand struct field update
The structEmployeeis created withTitle: "Developer". Then the fieldTitleis updated to"Senior Developer".Step 2: Identify printed value
The code printse.Titleafter update, so output isSenior Developer.Final Answer:
Senior Developer -> Option AQuick Check:
Updated struct field prints new value [OK]
- Expecting original value instead of updated
- Thinking field cannot be changed
- Confusing field name with value
Ready to prove it?
Pass the test and this pattern moves unknown
