How to create a pointer variable in Go or Golang?

August 25, 2022 - 4 min read

To create a pointer variable in Go or Golang, you can use the var keyword followed by the pointer variable name and then the * symbol (asterisk), and finally, the type of the variable you need to hold the memory address of without any whitespace.

TL;DR

package main

import "fmt"

func main() {
    // a simple variable
    name := "John Doe"

    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string

    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name

    // log the memory address
    fmt.Println(nameAddr) // 0xc000014250 <- this is the address in my system, yours may be different
}

For example, let's say we have a variable called name having the string of John Doe like this,

package main

func main(){
    // a simple variable
    name := "John Doe"
}

Now let's create a pointer variable to hold the memory address of the name variable. Let's call the pointer variable nameAddr.

It can be done like this,

package main

func main(){
    // a simple variable
    name := "John Doe"

    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string
}

As you can see from the above code we have used the var keyword followed by the pointer variable name of nameAddr, then the * symbol and the type of string without any whitespace. We have used the string type since we want to hold the memory address of the name variable which has the type of string.

Finally, to assign the memory address of the name variable, we can use the & symbol (ampersand) before the name variable which will get us the memory address of the name variable. We can then assign that value to the nameAddr variable using the = operator (assignment).

It can be done like this,

package main

func main(){
    // a simple variable
    name := "John Doe"

    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string

    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name
}

Now let's log the memory address of the name variable using the nameAddr pointer variable to the terminal like this,

package main

import "fmt"

func main() {
    // a simple variable
    name := "John Doe"

    // create a pointer variable to hold the
    // memory address of the `name` variable
    // using the `var` keyword, followed by the pointer variable name,
    // then the `*` symbol and then the type of the
    // variable to hold the memory address of
    var nameAddr *string

    // assign the memory address of the `name` variable
    // to the `nameAddr` pointer variable using
    // the `&` symbol
    nameAddr = &name

    // log the memory address
    fmt.Println(nameAddr) // 0xc000014250 <- this is the address in my system, yours may be different
}

We have successfully created a pointer variable in Go. Yay 🥳!

See the above code live in The Go Playground.

That's all 😃.

Feel free to share if you found this useful 😃.