How to Initialize Struct in Go: Syntax and Examples
In Go, you can initialize a
struct by using a struct literal with field names or by creating a variable and assigning values to its fields. You can also use the new keyword to create a pointer to a struct with zero values.Syntax
There are several ways to initialize a struct in Go:
- Struct literal with field names: Specify values for each field by name.
- Struct literal without field names: Provide values in order, but this is less clear.
- Using
newkeyword: Creates a pointer to a struct with zero values. - Declare variable and assign fields: Create an empty struct variable and set fields later.
go
type Person struct { Name string Age int } // Struct literal with field names p1 := Person{Name: "Alice", Age: 30} // Struct literal without field names p2 := Person{"Bob", 25} // Using new keyword p3 := new(Person) // p3 is *Person with zero values p3.Name = "Charlie" p3.Age = 40 // Declare variable and assign fields var p4 Person p4.Name = "Diana" p4.Age = 22
Example
This example shows how to create and print structs initialized in different ways.
go
package main import "fmt" type Person struct { Name string Age int } func main() { // Initialize with field names p1 := Person{Name: "Alice", Age: 30} // Initialize without field names p2 := Person{"Bob", 25} // Using new keyword p3 := new(Person) p3.Name = "Charlie" p3.Age = 40 // Declare variable and assign fields var p4 Person p4.Name = "Diana" p4.Age = 22 fmt.Println(p1) fmt.Println(p2) fmt.Println(*p3) fmt.Println(p4) }
Output
{{Alice 30}
{Bob 25}
{Charlie 40}
{Diana 22}}
Common Pitfalls
Common mistakes when initializing structs include:
- Omitting field names in struct literals can cause errors if the struct changes.
- Using
newreturns a pointer, so dereference is needed to access values. - Not initializing fields leaves them with zero values, which might be unexpected.
go
package main import "fmt" type Person struct { Name string Age int } func main() { // Wrong: omitting field names can be unclear // p := Person{"Eve"} // Error: missing Age field // Correct: use field names p := Person{Name: "Eve", Age: 28} // Using new returns pointer pPtr := new(Person) // Wrong: printing pointer directly fmt.Println(pPtr) // prints address // Correct: dereference pointer fmt.Println(*pPtr) // prints zero values }
Output
&{ 0}
{ 0}
Quick Reference
| Method | Description | Example |
|---|---|---|
| Struct literal with field names | Clear and safe way to initialize | p := Person{Name: "Alice", Age: 30} |
| Struct literal without field names | Less clear, order-dependent | p := Person{"Bob", 25} |
| Using new keyword | Creates pointer with zero values | p := new(Person) |
| Declare variable and assign fields | Initialize fields later | var p Person; p.Name = "Diana" |
Key Takeaways
Use struct literals with field names for clear and safe initialization.
The new keyword returns a pointer to a struct with zero values.
Avoid omitting field names to prevent errors when struct fields change.
You can declare a struct variable and assign fields later.
Dereference pointers returned by new to access struct values.