struct
struct (structure) is a data type in Go that groups multiple fields together. It is the central mechanism for achieving object-oriented design in Go, which has no classes — you can add methods to structs and express inheritance-like relationships through embedding.
Syntax
// Define a struct
type StructName struct {
FieldName Type
FieldName Type
}
// Initialization
s1 := StructName{value1, value2} // By field order (not recommended)
s2 := StructName{FieldName: value} // By field name (recommended)
var s3 StructName // Initialized to zero values
s4 := new(StructName) // Returns *StructName
// Accessing a field
s2.FieldName
// Struct embedding (promotes fields and methods of another struct, similar to inheritance)
type Parent struct { ... }
type Child struct {
Parent // Embedded without a field name
ExtraField Type
}
Struct Features
| Feature | Description |
|---|---|
| Field definition | Groups multiple values of different types into a single type. |
| Tags | Attaches metadata to fields (such as JSON key names). Enclosed in backticks. |
| Embedding | Writing only a type name promotes its fields and methods into the outer struct. |
| Anonymous struct | Defines a temporary struct without a type name. Useful for test data and similar one-off cases. |
Sample Code
package main
import "fmt"
// A basic struct definition.
type Person struct {
Name string
Age int
}
// An example of struct embedding.
type Address struct {
City string
Country string
}
type Employee struct {
Person // Promotes Person's fields and methods.
Address // Promotes Address's fields.
Department string
}
// Struct with JSON tags (used together with encoding/json)
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price,omitempty"` // Omitted when the value is zero.
}
func main() {
// Initialize a struct (using field names is recommended)
p := Person{Name: "Taro Yamada", Age: 30}
fmt.Printf("Name: %s, Age: %d\n", p.Name, p.Age)
// Modify a field via a pointer.
pp := &p
pp.Age = 31 // Same as (*pp).Age = 31. Go dereferences automatically.
fmt.Println("Age after birthday:", p.Age)
fmt.Println()
// Embedded structs
emp := Employee{
Person: Person{Name: "Hanako Suzuki", Age: 25},
Address: Address{City: "Tokyo", Country: "Japan"},
Department: "Engineering",
}
// Promoted fields can be accessed directly.
fmt.Println("Name:", emp.Name) // Same as emp.Person.Name
fmt.Println("City:", emp.City) // Same as emp.Address.City
fmt.Println("Department:", emp.Department)
fmt.Println()
// Anonymous struct (for temporary use such as test data)
point := struct {
X, Y int
}{X: 10, Y: 20}
fmt.Printf("Coordinates: (%d, %d)\n", point.X, point.Y)
}
Notes
Structs are used in place of classes in Go. Whether a field is exported (visible outside the package) depends on whether its name starts with an uppercase letter — uppercase means exported, lowercase means unexported. Structs are value types, so assignment and function arguments copy the entire value. Use a pointer when passing large structs to functions for better performance.
If a struct field name starts with a lowercase letter, it is invisible outside its package and cannot be handled by packages such as encoding/json or reflect. Always start field names with an uppercase letter if they need to be exported.
For adding methods to structs, see Method / Receiver. For combining structs with interfaces, see interface.
If you find any errors or copyright issues, please contact us.