Chapter 7
Defining the Blueprints of Your Data Using Structs
IN THIS CHAPTER
Defining structs for collections of items
Making a struct in Go
Copying a struct
Defining methods in structs
In the previous chapter, I tell you all about arrays and slices and show you how to group a collection of items together as a single unit. In this chapter, I fill you in on another way to represent your data: a struct. A struct (short for structure) is a used-defined type that allows you to group related data into a single logical unit. Here, I show you how to define and initialize a struct, make copies of a struct, define methods in a struct, and compare structs.
Defining Structs for a Collection of Items
Consider the following scenarios where you want to store the location of a point in a two-dimensional (2D) coordinate space. The x- and y-coordinates of the point can simply be stored using two variables:
pt1X := 3.1pt1Y := 5.7
If you have another point, you have another set of variables:
pt2X := 5.6pt2Y := 3.8
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access