Variables

Referencing in Go

Referencing is used to get the memory address of any variable.


Syntax
&variableName //can be used for any variable
pointerVariable := &variableName //creating a pointer variable using the address

Notes

Both pointers and standard variables can be referenced.


Example
package main

import "fmt"

func main() {
    animal := "cat"
    var pet *string
    pet = &animal //address of animal now in the pet pointer
    fmt.Println("My pet: ", *pet) //will print cat, value assigned to animal
    *pet = "dog" //assigning a new value to animal's address location, changing its value
    fmt.Println("Type of pet animal: ", animal)
}

See Also
Related

Pointers

Documentation

A Tour of Go

Add to Slack

< Pointers   |   Arrays >

© 2018 SyntaxDB. All Rights Reserved.