02-slices/

directory
v0.0.0-...-35bbffe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2015 License: Apache-2.0

README

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.

http://blog.golang.org/go-slices-usage-and-internals

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

Code Review

Declare and Length (Go Playground)

Reference Types (Go Playground)

Appending slices (Go Playground)

Taking slices of slices (Go Playground)

Strings and slices (Go Playground)

Variadic functions (Go Playground)

Advanced Code Review

Three index slicing (Go Playground)

Exercies

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)


Ardan Labs Ardan Studios GoingGo Blog


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

Directories

Path Synopsis
advanced
example1
Sample program to show how to use a third index slice.
Sample program to show how to use a third index slice.
Sample program to show how the capacity of the slice is not available for use.
Sample program to show how the capacity of the slice is not available for use.
Sample program to show the components of a slice.
Sample program to show the components of a slice.
Sample program to show how to takes slices of slices to create different views of and make changes to the underlying array.
Sample program to show how to takes slices of slices to create different views of and make changes to the underlying array.
Sample program to show how to grow a slice using the built-in function append and how append grows the capacity of the underlying array.
Sample program to show how to grow a slice using the built-in function append and how append grows the capacity of the underlying array.
Sample program to show how strings have a UTF-8 encoded byte array.
Sample program to show how strings have a UTF-8 encoded byte array.
Sample program to show how to declare and use variadic functions.
Sample program to show how to declare and use variadic functions.
exercises
exercise1
Declare a nil slice of integers.
Declare a nil slice of integers.
template1
Declare a nil slice of integers.
Declare a nil slice of integers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL