Home »
Golang
Go Structures
Last Updated : July 22, 2025
In Go, structures (structs) are user-defined types that allow you to group related data together. They are commonly used to create complex data models and encapsulate multiple fields under one name.
Defining a Structure
You can define a structure using the struct
keyword followed by a list of fields with their types.
Example
The following example defines a structure named Person
and creates an instance of it:
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
p1 := Person{name: "Alice", age: 30}
fmt.Println("Name:", p1.name)
fmt.Println("Age:", p1.age)
}
When you run the above code, the output will be:
Name: Alice
Age: 30
Here,
Person
is a struct with two fields: name
and age
.
p1
is a variable of type Person
initialized with values.
Accessing and Modifying Struct Fields
You can access and modify struct fields using the dot notation.
Example
The following example demonstrates how to create a structure, assign values to its fields, and print the updated structure:
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
var p Person
p.name = "Bob"
p.age = 25
fmt.Println("Updated Person:", p)
}
When you run the above code, the output will be:
Updated Person: {Bob 25}
Passing Struct to Functions
Structs in Go are passed by value, meaning a copy is passed to the function. To modify the original, use a pointer.
Example (By Value)
The following example demonstrates how passing a structure by value to a function does not modify the original structure:
package main
import "fmt"
type Person struct {
name string
age int
}
func updateAge(p Person) {
p.age = 40
fmt.Println("Inside function:", p)
}
func main() {
person := Person{name: "Charlie", age: 35}
updateAge(person)
fmt.Println("In main:", person)
}
When you run the above code, the output will be:
Inside function: {Charlie 40}
In main: {Charlie 35}
Example (By Reference)
The following example demonstrates how passing a structure by reference using a pointer allows modification of the original structure:
package main
import "fmt"
type Person struct {
name string
age int
}
func updateAgeByRef(p *Person) {
p.age = 40
fmt.Println("Inside function:", *p)
}
func main() {
person := Person{name: "Charlie", age: 35}
updateAgeByRef(&person)
fmt.Println("In main:", person)
}
When you run the above code, the output will be:
Inside function: {Charlie 40}
In main: {Charlie 40}
Anonymous Fields in Struct
Go allows struct fields without names. The type itself serves as the field name.
Example
The following example demonstrates how to define a structure with unnamed (anonymous) fields and access them using their type names:
package main
import "fmt"
type Person struct {
string
int
}
func main() {
p := Person{"David", 28}
fmt.Println("Name:", p.string)
fmt.Println("Age:", p.int)
}
When you run the above code, the output will be:
Name: David
Age: 28
Key Points of Structures
- Structures in Go are created using the
struct
keyword.
- They can group multiple fields of different types under one name.
- Structs are passed by value unless pointers are used.
- You can define anonymous fields in structs.
Go Structures Exercise
Select the correct option to complete each statement about structures in Go.
- In Go, structures are defined using the:
- When a struct is passed to a function without a pointer:
- Anonymous fields in a struct use:
Advertisement
Advertisement