Slide 1

Slide 1 text

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/

Slide 2

Slide 2 text

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/

Slide 3

Slide 3 text

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/

Slide 4

Slide 4 text

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/

Slide 5

Slide 5 text

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/

Slide 6

Slide 6 text

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/

Slide 7

Slide 7 text

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/

Slide 8

Slide 8 text

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/

Slide 9

Slide 9 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」 - Pointers 9 / 9 Pointers in Go Luca Sepe Software Craftsman, Solution Architect, Trainer https://twitter.com/lucasepe/ https://github.com/lucasepe/ https://lucasepe.it/