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
Get Go Programming Language For Dummies 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.