Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 2.75 KB

README.md

File metadata and controls

50 lines (36 loc) · 2.75 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

http://blog.golang.org/go-slices-usage-and-internals
https://blog.golang.org/strings
http://blog.golang.org/slices
http://www.goinggo.net/2013/08/understanding-slices-in-go-programming.html
http://www.goinggo.net/2013/08/collections-of-unknown-length-in-go.html
http://www.goinggo.net/2013/09/iterating-over-slices-in-go.html
http://www.goinggo.net/2013/09/slices-of-slices-of-slices-in-go.html
http://www.goinggo.net/2013/12/three-index-slices-in-go-12.html
https://github.com/golang/go/wiki/SliceTricks

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)

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.