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

A Simple Overview of Arrays in Go

A Simple Overview of Arrays in Go

Luca Sepe

May 26, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. Day #5 - Arrays A simple overview of arrays in

    Go 「 Trying to make you to fall in love with Golang 」 1 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  2. What is an Array An array is a collection of

    the same data type. has a fixed length - the size of an array cannot vary The literal representation of an array type is [n]T , where n is the size and T is the element type. Try it! 「Trying to make you to fall in love with Golang」 - Arrays 2 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  3. Letting the compiler deducing the length We can use the

    ... operator in place of size n in [n]T array type syntax. Go will deduce the size of the array Try It! 「Trying to make you to fall in love with Golang」 - Arrays 3 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  4. Nested Composite Literals Can Be Simplified If a composite literal

    nested many other composite literals, then those nested composited literals can simplified to the form {...} Try It! 「Trying to make you to fall in love with Golang」 - Arrays 4 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  5. Array comparison ( == ) Two arrays are equal if

    ... they are of the same type contains the same values all the values are in the same order Try It! 「Trying to make you to fall in love with Golang」 - Arrays 5 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  6. Array iteration To iterate over an array we can use

    a classic C-style for loop the range operator which returns index and value for each element in the array Try It! 「Trying to make you to fall in love with Golang」 - Arrays 6 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  7. Arrays in Go are value types When you assign an

    array to a new variable or pass an array to a function, the entire array is copied. any changes to this copied array, won't affect the original array Try It! 「Trying to make you to fall in love with Golang」 - Arrays 7 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  8. 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」 - Arrays 8 / 8 Arrays Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/