6. Structs, Methods, and Pointers

In this chapter, we look at structs in Go. We cover definition, initialization, and usage of structs. We also cover struct tags and their use for operations such as encoding and decoding. Next, we look at adding methods to types. Finally, we examine pointers and how you can use them to avoid copying data and how they enable you to allow others to mutate the underlying data.

Structs

A struct is a collection of fields, often called members (or attributes). A struct is often used to create custom complex types in Go. When trying to understand structs, it helps to think of them as a blueprint for what the new type will do. A struct definition does not contain any data.

Declaring New Types in Go

Before you can ...

Get Go Fundamentals: Gopher Guides now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.