slices/

directory
v0.0.0-...-38a5715 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 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.

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.

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 one needs to be careful when appending to a slice when you have a reference to an element.
Sample program to show how one needs to be careful when appending to a slice when you have a reference to an element.
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.
Sample program to show how the for range has both value and pointer semantics.
Sample program to show how the for range has both value and pointer semantics.
Sample program to show how slices allow for efficient linear traversals.
Sample program to show how slices allow for efficient linear traversals.
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