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/
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/
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/
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/
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/
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/
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/
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/
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/
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/
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/
(@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/