Last modified: Jan 23, 2026 By Alexander Williams
Using Variables in Go Structs: A Guide
Structs are the building blocks for data in Go.
They group variables together to form a single, logical unit.
This article explains how to work with variables inside these structs.
You will learn to define, create, and modify struct data effectively.
What is a Struct in Go?
A struct is a composite data type.
It allows you to combine variables of different types.
These variables are called fields.
Think of a struct as a blueprint for creating custom data records.
Defining a Struct
You define a struct using the type and struct keywords.
Inside the curly braces, you list the field names and their types.
// Define a struct type named 'Person'
type Person struct {
Name string
Age int
City string
}
Here, Person is a new type.
It has three fields: Name, Age, and City.
Field names starting with an uppercase letter are exported.
This means other packages can access them.
For more on visibility, see the Go Exported vs Unexported Variables Guide.
Creating and Initializing Structs
Once defined, you create variables of your struct type.
There are several ways to initialize them.
1. Using a Zero Value Struct
Declare a variable. Go assigns zero values to all fields.
var p1 Person
fmt.Printf("P1: %+v\n", p1) // Fields are zero values
P1: {Name: Age:0 City:}
2. Using a Struct Literal
You can set values during creation.
// Method 1: Specify field names (recommended)
p2 := Person{
Name: "Alice",
Age: 30,
City: "Berlin",
}
// Method 2: Rely on field order (less readable)
p3 := Person{"Bob", 25, "London"}
fmt.Println("P2:", p2)
fmt.Println("P3:", p3)
P2: {Alice 30 Berlin}
P3: {Bob 25 London}
Accessing and Modifying Struct Fields
Use the dot (.) operator to work with fields.
employee := Person{Name: "Charlie", Age: 40}
fmt.Println("Name:", employee.Name) // Access
employee.Age = 41 // Modify
employee.City = "Paris" // Assign a new value
fmt.Printf("Updated: %+v\n", employee)
Name: Charlie
Updated: {Name:Charlie Age:41 City:Paris}
This is similar to reassigning variables in Go functions.
You are changing the value stored in a specific variable field.
Pointers to Structs
You often work with pointers to structs.
This is efficient for large structs or when you need to modify the original.
// Create a pointer to a struct
ptr := &Person{Name: "Diana"}
// Access fields using the pointer with the dot operator
// Go automatically dereferences for you.
ptr.Age = 28
fmt.Println("Via Pointer:", ptr.Name, "is", ptr.Age)
// Explicit dereferencing also works
(*ptr).City = "Rome"
fmt.Println("Explicit:", (*ptr).City)
Via Pointer: Diana is 28
Explicit: Rome
For a deeper dive, read the Go Pointers with Variables: Simple Guide.
Embedded Structs and Promotion
Go supports embedding one struct within another.
Fields of the embedded struct are promoted to the outer struct.
type Contact struct {
Email string
Phone string
}
type Employee struct {
Person // Embedded struct
Contact // Embedded struct
Salary float64
}
// Create an Employee
emp := Employee{
Person: Person{Name: "Eve", Age: 35},
Contact: Contact{Email: "[email protected]", Phone: "12345"},
Salary: 75000.0,
}
// Access promoted fields directly
fmt.Println("Name:", emp.Name) // From Person
fmt.Println("Email:", emp.Email) // From Contact
Name: Eve
Email: [email protected]
Anonymous (Inline) Structs
You can define a struct without a type name.
This is useful for one-time use, especially in JSON handling.
// Define and use an anonymous struct
config := struct {
Host string
Port int
}{
Host: "localhost",
Port: 8080,
}
fmt.Println("Config:", config.Host, config.Port)
Config: localhost 8080
Best Practices and Common Pitfalls
Follow these tips for clean and maintainable code.
Use descriptive field names. Clarity is key.
Prefer the field-name initialization syntax. It's more readable and resilient to struct changes.
Consider using pointers for large structs or required modifications. It avoids copying data.
Be mindful of variable scope, especially when creating structs inside loops. Learn more about Go Variable Scope in for Loops Explained.
Avoid using global variables for struct instances unless necessary, as discussed in the Go Global Variables: Safe Usage Guide.
Conclusion
Structs organize related variables into meaningful groups.
You now know how to define struct types and create variables from them.
You can initialize, access, and modify their fields.
You understand pointers, embedding, and anonymous structs.
Mastering structs is a fundamental step toward writing effective Go programs.
Use them to model your application's data cleanly and efficiently.