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

Pointers in Go

Pointers in Go

Luca Sepe

May 25, 2020
Tweet

More Decks by Luca Sepe

Other Decks in Programming

Transcript

  1. Day #4 - Pointers A pointer is a variable that

    contains another variable's memory address. 「 Trying to make you to fall in love with Golang 」 1 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  2. What is a Variable? A variable is a "tagged box"

    that holds information. This "tagged box" in memory has a specific address, represented by a unique number The picture on the right shows a variable "tagged" foo wich contains the value 10 and is located at 0xc00002c008 . 「Trying to make you to fall in love with Golang」 - Pointers 2 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  3. What is a Pointer? A pointer is a variable that

    contains another variable's memory address. Writing bar := &foo we are defining a pointer to the foo variable. bar is a pointer - it points to the memory address of foo &foo gives the address of the variable ("box") whose "tag" is foo 「Trying to make you to fall in love with Golang」 - Pointers 3 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  4. Try It! 4 / 9 Pointers in Go Luca Sepe

    Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  5. Dereferencing a pointer To take or update the value pointed

    by bar we need to "follow that pointer" that is "dereferencing" or "indirecting" that pointer *bar gets the value stored at the address pointed by bar *bar = XXX updates the value stored at the address pointed by bar 「Trying to make you to fall in love with Golang」 - Pointers 5 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  6. Automatic Dereference of Pointers to Structs Since g is a

    pointer, we should use *g dereferencing syntax, like this (*g).Title , to get the struct field value. But Go allows us to access the fields of a struct pointer without dereferencing it, using the simplest syntax g.Title Go will take care of dereferencing a pointer under the hood Try It! 6 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  7. The built-in new function The new function allows us to

    create a pointer. allocates enough memory to accommodate a value of that type returns a pointer to it Try it! 「Trying to make you to fall in love with Golang」 - Pointers 7 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  8. Returning Pointers of Local Variables is Safe Go is a

    language supporting garbage collection, so return the address of a local variable is absolutely safe. Try It! 「Trying to make you to fall in love with Golang」 - Pointers 8 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/
  9. 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」 - Pointers 9 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/