Skip to main content

How to Define a Struct in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A struct groups related values under one type: a book's title, author and year. In Go you declare the struct type once, then create as many values of it as you need.

Syntax​

Syntax
type <Name> struct {
<Field> <type>
<Field> <type>
}

<value> := <Name>{<Field>: <value>, <Field>: <value>}
PartWhat it does
type <Name> struct { ... }Declares a new struct type with its fields.
<Field> <type>One field: a name and its type.
<Name>{<Field>: <value>}A composite literal: creates a value, setting fields by name.

The Go specification: "A struct is a sequence of named elements, called fields, each of which has a name and a type."

The Example​

The example declares a Book type, creates one book, prints its title and then the whole struct.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Struct​

Create a file named main.go:

main.go, which declares a Book struct with Title, Author and Year, creates Dune by Frank Herbert from 1965, prints book.Title and prints the book with %+v.

main.go
package main

import "fmt"

type Book struct {
Title string
Author string
Year int
}

func main() {
book := Book{Title: "Dune", Author: "Frank Herbert", Year: 1965}
fmt.Println(book.Title)
fmt.Printf("%+v\n", book)
}

%+v prints a struct with its field names, which is handy for checking what it holds.

Step 2: Run It​

Terminal
go run main.go
Output
Dune
{Title:Dune Author:Frank Herbert Year:1965}

A zsh terminal where go run main.go prints Dune and the book struct with Title, Author and Year.

The command is the same in zsh, bash and PowerShell.

Fields You Leave Out​

Fields you don't set get their type's zero value: "" for strings and 0 for numbers. Book{} printed with %+v gives {Title: Year:0} for a struct with only Title and Year.

Common Mistakes​

MistakeWhat happensDo this instead
Setting a field the struct doesn't have, as in Pages: 412unknown field Pages in struct literal of type BookAdd the field to the type, or remove it from the literal
Lowercase field names, such as titleOther packages, including encoding/json, can't see themCapitalize fields that must be visible outside
Creating a value without field names, as in Book{"Dune", "Frank Herbert", 1965}It breaks when fields are added or reorderedName the fields

Troubleshooting​

SymptomLikely causeFix
unknown field … in struct literalA misspelled or missing fieldCompare with the type's fields
A field is empty or 0It wasn't set in the literalSet it, or rely on its zero value on purpose

FAQ​

How do I change a field?​

Assign to it: book.Year = 1966. To change it inside a function, pass a pointer to the struct.

Can structs have methods?​

Yes: func (b Book) Summary() string { ... } gives every Book a Summary method.

Conclusion​

type Name struct { ... } declares the fields, Name{Field: value} creates a value, and value.Field reads one. Unset fields start at their zero value.

Additional Resources​