Chapter 8. building storage: Structs

image

Sometimes you need to store more than one type of data.

We learned about slices, which store a list of values. Then we learned about maps, which map a list of keys to a list of values. But both of these data structures can only hold values of one type. Sometimes, you need to group together values of several types. Think of billing receipts, where you have to mix item names (strings) with quantities (integers). Or student records, where you have to mix student names (strings) with grade point averages (floating-point numbers). You can’t mix value types in slices or maps. But you can if you use another type called a struct. We’ll learn all about structs in this chapter!

Slices and maps hold values of ONE type

Gopher Fancy is a new magazine devoted to lovable rodents. They’re currently working on a system to keep track of their subscriber base.

image
image

It’s true: arrays, slices, and maps are no help if you need to mix values of different types. They can only be set up to hold values of a single type. But Go does have a way to solve this problem...

Structs are built out of values of MANY types

A struct (short for “structure”) is a value that is ...

Get Head First Go 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.