Skip to content

Latest commit

 

History

History
52 lines (38 loc) · 3.48 KB

README.md

File metadata and controls

52 lines (38 loc) · 3.48 KB

Slices - Arrays, Slices and Maps

Slices are an incredibly important data structure in Go. They form the basis for how we manage and manipulate data in a flexible, performant and dynamic way. It is incredibly important for all Go programmers to learn how to uses slices.

Notes

  • Slices are like dynamic arrays with special and built-in functionality.
  • There is a difference between a slices length and capacity and they each service a purpose.
  • Slices allow for multiple "views" of the same underlying array.
  • Slices can grow through the use of the built-in function append.

Links

Go Slices: usage and internals - Andrew Gerrand
Strings, bytes, runes and characters in Go - Rob Pike
Arrays, slices (and strings): The mechanics of 'append' - Rob Pike
Understanding Slices in Go Programming - William Kennedy
Collections Of Unknown Length in Go - William Kennedy
Iterating Over Slices In Go - William Kennedy
Slices of Slices of Slices in Go - William Kennedy
Three-Index Slices in Go 1.2 - William Kennedy
SliceTricks
runtime: Make slice growth formula a bit smoother - Go Team

Code Review

Declare and Length (Go Playground)
Reference Types (Go Playground)
Appending slices (Go Playground)
Taking slices of slices (Go Playground)
Slices and References (Go Playground)
Strings and slices (Go Playground)
Variadic functions (Go Playground)
Range mechanics (Go Playground)
Efficient Traversals (Go Playground)

Advanced Code Review

Three index slicing (Go Playground)

Exercises

Exercise 1

Part A Declare a nil slice of integers. Create a loop that appends 10 values to the slice. Iterate over the slice and display each value.

Part B Declare a slice of five strings and initialize the slice with string literal values. Display all the elements. Take a slice of index one and two and display the index position and value of each element in the new slice.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.