Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Slices in Go

Slices in Go

Luca Sepe

May 27, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. 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/
  2. 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/
  3. 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/
  4. 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/
  5. 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/
  6. 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/
  7. 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/
  8. 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/
  9. 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/
  10. 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/
  11. 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/
  12. 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/