notexOPENOverview
Notex/Languages/Composite Types

Composite Types

Always free to read

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

GO / CORE LANGUAGE

Composite Types

33 LESSONS · 62 MIN
  1. 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…READING3 MIN
  2. 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…READING2 MIN
  3. 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…READING1 MIN
  4. 01.04Growing slices with appendThe built-in append function is used to grow slices:READING1 MIN
  5. 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…READING3 MIN
  6. 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…READING2 MIN
  7. 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:READING1 MIN
  8. 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…READING2 MIN
  9. 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…READING4 MIN
  10. 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…READING4 MIN
  11. 01.11Copying Slices of SlicesSlice expressions allow you to copy from the middle of the source slice:READING1 MIN
  12. 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…READING1 MIN
  13. 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.READING2 MIN
  14. 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…READING2 MIN
  15. 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…READING1 MIN
  16. 01.16Code Review: Slices and ArraysUse slices instead of arrays, unless the number of elements never changes.PRACTICE1 MIN
  17. 01.17Practice: Slices and ArraysThe following exercises will help solidify your knowledge of slices.PRACTICE1 MIN
  18. 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…READING6 MIN
  19. 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…READING2 MIN
  20. 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…READING1 MIN
  21. 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.READING1 MIN
  22. 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…READING1 MIN
  23. 01.23Deleting from MapsKey-value pairs are removed from a map via the built-in delete function:READING1 MIN
  24. 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:READING1 MIN
  25. 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…READING1 MIN
  26. 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…READING2 MIN
  27. 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…READING3 MIN
  28. 01.28Code Review: Strings and MapsDon’t slice or index a string unless you know it’s single-byte content.PRACTICE1 MIN
  29. 01.29Practice: Strings and MapsThe following exercises cover strings and maps.PRACTICE1 MIN
  30. 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.…READING6 MIN
  31. 01.31Code Review: StructsUse structs to group related data.PRACTICE1 MIN
  32. 01.32Practice: StructsThe following exercises test what you’ve learned about structs.PRACTICE1 MIN
  33. 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…READING1 MIN
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K