DEV Community

Cover image for How to create a pointer variable in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to create a pointer variable in Go or Golang?

#go

Originally posted here!

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
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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 πŸ˜ƒ.


Top comments (0)