Slide 1

Slide 1 text

Day #6 - Slices A simple introduction to slices in Go 「 Trying to make you to fall in love with Golang 」 1 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 2

Slide 2 text

What are Slices? Slices are build on arrays to provide great power and convenience. unlike arrays, they can be resized using the built-in append function A slice type consists of: a pointer to the array the length of the segment its capacity - the maximum length of the segment 「Trying to make you to fall in love with Golang」 - Slices 2 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 3

Slide 3 text

Declaring and Creating Slices A slice of type T : is declared using []T var a []int is created using []T{ el1, el2,...elN} var s = []int{1, 2, 3} s := []int{1, 2, 3} Just like an array but without specifying any size in the brackets [] . Try it! 「Trying to make you to fall in love with Golang」 - Slices 3 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 4

Slide 4 text

Creating a Slice from an existing Array or Slice We have to specify two indices low (lower bound) and high (upper bound) separated by a colon - a[low:high] the low and high indices in the slice expression are optional default value for low is 0 default value for high is the length of the slice Try It! 4 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 5

Slide 5 text

Modifying a slice Changing the elements of a slice will modify the corresponding elements in the referenced array. other slices that refer the same array will also see those modifications Try It! 「Trying to make you to fall in love with Golang」 - Slices 5 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 6

Slide 6 text

Length and Capacity A slice is a struct with three fields: a pointer to the underlying array the length of the segment of the array that the slice contains the capacity (the maximum size up to which the segment can grow) We can find the length and capacity using the built- in functions len() and cap() . Try It! 6 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 7

Slide 7 text

The built-in make function We can also use the function make([]T, len, cap) []T) to create slices. It takes a type, a length, and an optional capacity. allocates an underlying array with size equal to the given capacity returns a slice that refers to that array Try It! 「Trying to make you to fall in love with Golang」 - Slices 7 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 8

Slide 8 text

Copying a Slice To copy elements from one slice to another we can use the built-in copy function. takes two slices, a destination and a source returns the number of elements that are copied the minimum of len(src) and len(dst) Try It! 「Trying to make you to fall in love with Golang」 - Slices 8 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 9

Slide 9 text

Adding elements to a Slice The built-in append function adds new elements at the end of a given slice. takes a slice and a variable number of arguments of type T returns a new slice containing all the elements from the given slice plus the new ones Try It! 「Trying to make you to fall in love with Golang」 - Slices 9 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 10

Slide 10 text

append gotcha Since the append function manipulates the underlying array, creating new slice variables based on this function can lead to sneaky bugs. use append only to append new value to given slice to create a new slice based on old one with some extra appended value, always copy it first Try It! 「Trying to make you to fall in love with Golang」 - Slices 10 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 11

Slide 11 text

Iterating over a Slice We can iterate over a slice like we do over arrays: using a for loop using the for range use the blank operator _ to ignore the index Try It! 「Trying to make you to fall in love with Golang」 - Slices 11 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/

Slide 12

Slide 12 text

Thank you Luca Sepe Solution Architect (@day) / Software Craftsman (@night) {twitter.com, github.com, linkedin.com} / lucasepe 「Trying to make you to fall in love with Golang」 - Slices 12 / 12 Slices in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/