Go Structs Explained for Beginners

Updated 4 min read
Go Structs featured image: cards showing struct fields, methods and JSON tags

Go structs let you group related data into one custom type, like a student with a name, age and grade. In this post, you will learn to define structs, create values, add methods and turn them into JSON. Each idea comes with a short example that you can run yourself.

So far, a map in Go or a slice in Go held many values of one type. Structs are different, because each field can have its own type. You will also attach functions to them, so a quick look at writing and calling functions in Go will help.

What Are Go Structs?

A struct is a collection of named fields. A field is one piece of data with its own name and type. Think of a bank form with boxes for name, age and address. Each box holds a different kind of data.

Go has no classes like Java. Instead, Go structs plus methods do the same job in a simpler way. As a result, almost every real Go program uses them to model users, orders or products.

Defining and Creating Go Structs

type Student struct {
	Name  string
	Age   int
	Grade string
}

s1 := Student{Name: "Asha", Age: 20, Grade: "A"}
fmt.Println(s1.Name, s1.Age) // Asha 20

The type keyword creates a new type called Student. Think of it as a blueprint. Then Student{...} builds a real value from that blueprint, with each field named for clarity.

Accessing and Changing Fields

s1.Age = 21
fmt.Println(s1) // {Asha 21 A}

Use a dot to read or change any field in Go structs. Also, field names that start with a capital letter are visible to other packages, while lowercase names stay private.

Zero Values in Go Structs

var s2 Student
fmt.Printf("%+v\n", s2) // {Name: Age:0 Grade:}

A struct you never fill still works. Each field simply gets its zero value, so text is empty and numbers are 0. The %+v verb prints field names too, which helps a lot when you debug.

Pointers to Structs

p := &s1
p.Grade = "A+"
fmt.Println(s1.Grade) // A+

A pointer holds the address of a value instead of a copy of it. The & sign gets that address. So changing p.Grade changes s1 itself, and Go lets you use the dot without any extra symbols.

Adding Methods to Go Structs

type Account struct {
	Owner   string
	Balance float64
}

func (a Account) Show() {
	fmt.Println(a.Owner, "has", a.Balance)
}

func (a *Account) Deposit(amount float64) {
	a.Balance += amount
}

acc := Account{Owner: "Ravi", Balance: 100}
acc.Deposit(50)
acc.Show() // Ravi has 150

A method is a function tied to a type. The part in brackets before the name, like (a Account), is called the receiver. It tells Go which type owns the method.

Value vs Pointer Receivers

Show uses a value receiver, so it gets a copy and only reads data. Deposit uses a pointer receiver, *Account, so it can change the original balance. In short, use a pointer receiver whenever a method must update the struct.

Go Structs diagram showing a struct type, a value and value versus pointer receivers

The type is the blueprint, acc is the value, and only the pointer receiver can change it.

Embedding One Struct in Another

type Address struct {
	City string
}

type Employee struct {
	Name string
	Address
}

e := Employee{Name: "Meera", Address: Address{City: "Pune"}}
fmt.Println(e.City) // Pune

When you list a type without a field name, Go embeds it. Then its fields act as if they belong to the outer struct, so e.City works directly. This is how Go reuses code without inheritance.

Struct Tags and JSON

type Product struct {
	Name  string  `json:"name"`
	Price float64 `json:"price"`
}

b, _ := json.Marshal(Product{Name: "Pen", Price: 20})
fmt.Println(string(b)) // {"name":"Pen","price":20}

The text in backticks is a struct tag. Here it tells the encoding/json package which name to use for each field. Later in this series, you will use this to build real APIs.

Common Mistakes with Go Structs

Error you seeWhy it happensHow to fix it
unknown field Nme in struct literal of type StudentThe field name has a typo.Match the name exactly.
too few values in struct literal of type StudentThe literal skips names and misses some values.Use Name: value pairs.
Method change is lostThe method has a value receiver.Change it to a pointer receiver.

The lost change is the trickiest one, because Go shows no error at all. Receiver questions also come up often in my Go interview questions and answers, so practise them there.

Official Resources

These pages come straight from the Go team, so they are always accurate and up to date.

Quick Recap

  • Go structs group fields of different types into one custom type.
  • Create values with named fields, and empty fields get zero values.
  • Methods attach behaviour to a type through a receiver.
  • Use a pointer receiver when a method must change the struct.
  • Embedding reuses fields, and tags control JSON names.

What’s Next?

You used pointers briefly in this post. Next, we will look at them properly, so you know exactly when Go copies data and when it shares it.

Coming soon: Go Pointers Explained Simply