Structs in Go

Contents
Define a struct
Composite literals
Access fields
Compare and copy

Define a struct

A struct groups related fields of different types under one named type. Define it once with the type keyword, then every variable of that type carries the same set of fields.

package main import "fmt" type Point struct { X int Y int } func main() { var p Point fmt.Println(p) }

go run main.go

{0 0}

A fresh struct holds the zero value in every field, so printing it shows {0 0} here. Field names start with a capital letter when other packages must see them.

Composite literals

Composite literals build a value and set fields in one expression. Name the fields for clarity, or list values in field order for very short throwaway values.

package main import "fmt" type Point struct { X int Y int } func main() { p1 := Point{X: 1, Y: 2} p2 := Point{3, 4} fmt.Println(p1, p2) }

{1 2} {3 4}

Field names protect against reordering: adding a field in the middle breaks positional literals but leaves named ones working. Prefer named fields in real code.

Access fields

Reach a field with a dot: read it into expressions or assign a fresh value through the same path. Nested structs chain dots, one per level.

package main import "fmt" type User struct { Name string Age int } func main() { u := User{Name: "Ada", Age: 36} u.Age = 37 fmt.Println(u.Name, u.Age) }

Ada 37

The dot works through pointers too: Go follows the pointer automatically, so u.Age reads the same whether u is a struct or a pointer to one.

Compare and copy

Structs without slice or map fields compare with == and copy field by field on assignment. That value behavior makes small records safe to duplicate and test for equality.

package main import "fmt" type Point struct { X int Y int } func main() { a := Point{X: 1, Y: 2} b := a fmt.Println(a == b) b.X = 9 fmt.Println(a, b) }

true
{1 2} {9 2}

Assignment copies the whole value, so later changes to b leave a untouched. Use pointers from the previous lesson when many copies would cost too much or shared state is the goal.

Next: Methods

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: