How to create a pointer variable in Go or Golang?
Tags: go
Published: 2022-08-26 15:13:41

---
  title: How to create a pointer variable in Go or Golang?
  published: true
  tags: Go
  date: Fri Aug 26 2022 20:43:41 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/create-pointer-variable-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-a33fc955f10dc55095b432deeacde655.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/create-pointer-variable-go-golang)
  
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

```go
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,

```go
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,

```go
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,

```go
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,

```go
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](https://go.dev/play/p/bKyp8-uQhZz).

That's all 😃.

### Feel free to share if you found this useful 😃.

---