Like most programming languages, Go has arrays. However, arrays are rarely used directly in Go. You’ll learn why in a bit, but first let’s quickly cover array declaration…
- 01.01Arrays—Too Rigid to Use DirectlyLike most programming languages, Go has arrays. However, arrays are rarely used directly in Go. You’ll learn why in a bit, but first let’s quickly cover array declaration…
- 01.02SlicesMost of the time, when you want a data structure that holds a sequence of values, a slice is what you should use. What makes slices so useful is that you can grow slices as…
- 01.03Measuring slice lengthGo provides several built-in functions to work with slices. You’ve already seen the built-in len function when looking at arrays. It works for slices too. Passing a nil slice…
- 01.04Growing slices with appendThe built-in append function is used to grow slices:
- 01.05CapacityAs you’ve seen, a slice is a sequence of values. Each element in a slice is assigned to consecutive memory locations, which makes it quick to read or write these values. The…
- 01.06Allocating slices with makeYou’ve already seen two ways to declare a slice, using a slice literal or the nil zero value. While useful, neither way allows you to create an empty slice that already has a…
- 01.07Emptying a SliceGo 1.21 added a clear function that takes in a slice and sets all of the slice’s elements to their zero value. The length of the slice remains unchanged. The following code:
- 01.08Declaring Your SliceNow that you’ve seen all these ways to create slices, how do you choose which slice declaration style to use? The primary goal is to minimize the number of times the slice…
- 01.09Sharing and Copying SlicesAnother difference between slices and arrays is that assigning an array variable to another array variable creates an independent copy of the array, while assigning a slice…
- 01.10Slicing SlicesA slice expression creates a slice from a slice. It’s written inside brackets and consists of a starting offset and an ending offset, separated by a colon (:). The starting…
- 01.11Copying Slices of SlicesSlice expressions allow you to copy from the middle of the source slice:
- 01.12Converting Arrays to SlicesSlices aren’t the only thing you can slice. If you have an array, you can take a slice from it using a slice expression. This is a useful way to bridge an array to a function…
- 01.13Converting Slices to ArraysUse a type conversion to make an array variable from a slice. You can convert an entire slice to an array of the same type, or you can create an array from a subset of the slice.
- 01.14Comparing SlicesAs I mentioned earlier, slices are not comparable, which means they cannot be compared to each other using the == and != operators (slices can only be compared to nil). If you…
- 01.15Other Slice FunctionsThe slices package in the Go Standard Library includes many other functions to make it easier to work with slices. If you find yourself writing a function to manipulate or…
- 01.16Code Review: Slices and ArraysUse slices instead of arrays, unless the number of elements never changes.
- 01.17Practice: Slices and ArraysThe following exercises will help solidify your knowledge of slices.
- 01.18Strings and Runes and BytesNow that I’ve talked about slices, we can look at strings again. You might think that a string in Go is made out of runes, but that’s not the case. Under the covers, Go uses a…
- 01.19MapsSlices are useful when you have sequential data. Like most languages, Go provides a built-in data type for situations where you want to associate one value to another. The map…
- 01.20What Is a Hash Map?In computer science, a map is a data structure that associates (or maps) one value to another. Maps can be implemented in several ways, each with its own trade-offs. The map…
- 01.21Reading and Writing a MapLet’s look at a short program that declares, writes to, and reads from a map. You can run the program in Example 2-10 on The Go Playground.
- 01.22The comma ok IdiomAs you’ve seen, a map returns the zero value if you ask for the value associated with a key that’s not in the map. This is handy when implementing things like the totalWins…
- 01.23Deleting from MapsKey-value pairs are removed from a map via the built-in delete function:
- 01.24Emptying a MapThe clear function that you saw in “Emptying a Slice” works on maps also. A cleared map has its length set to zero, unlike a cleared slice. The following code:
- 01.25Comparing MapsGo 1.21 added a package to the standard library called maps that contains helper functions for working with maps. Two functions in the package are useful for comparing if two…
- 01.26Copying MapsAssigning a map variable to another map variable shares the map between the two variables. If you modify one variable, the other is modified as well. This is similar to the way…
- 01.27Using Maps as SetsMany languages include a set in their standard library. A set is a data type that ensures there is at most one of a value, but doesn’t guarantee that the values are in any…
- 01.28Code Review: Strings and MapsDon’t slice or index a string unless you know it’s single-byte content.
- 01.29Practice: Strings and MapsThe following exercises cover strings and maps.
- 01.30StructsMaps are a convenient way to store some kinds of data, but they have limitations. They don’t define an API since there’s no way to constrain a map to allow only certain keys.…
- 01.31Code Review: StructsUse structs to group related data.
- 01.32Practice: StructsThe following exercises test what you’ve learned about structs.
- 01.33Recap: Composite TypesYou’ve learned a lot about composite types in Go. In addition to learning more about strings, you now know how to use the built-in container types: slices and maps. You can…