O'Reilly logo

Head First Go by Jay McGavren

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8 structs

Building Storage

Images

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 mailing addresses, where you have to mix street names (strings) with postal codes (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.

Images

Images

Structs are built out of values of MANY types

A struct (short for "structure") is a value that is constructed out of other values of many different types. Whereas a slice might only be able to hold string values or a map might only be able to hold int values, you can create a struct that holds string values, int ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required