# Dereferencing pointer

**URL:** <https://forum.golangbridge.org/t/dereferencing-pointer/21201>\
**Category:** Technical Discussion\
**Created:** [November 3, 2020, 12:47pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201 "2020-11-03T12:47:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shantanu\_Shekhar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/shantanu_shekhar/32/8665_2.png) [@Shantanu\_Shekhar](https://forum.golangbridge.org/u/Shantanu_Shekhar)\
**Post date:** [November 3, 2020, 12:47pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/1 "2020-11-03T12:47:27Z")

</div>

The code below requires that I use `personPtr.age = personPtr.age + 1`. I was thinking I would have to dereference the pointer like so `*personPtr.age = *personPtr.age + 1` but the compiler complains about this latter option. Why do I not need to dereference the pointer?

```auto
package main

import (
	"fmt"
)

type person struct {
	name string
	age int
}

func happyBirthday(personPtr *person) {
	personPtr.age = personPtr.age + 1
}

func main() {
	youngMonkey := person{name: "YoungMonkey", age: 0}
	happyBirthday(&youngMonkey)
	fmt.Println(youngMonkey.age)
}

```

---

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/hollowaykeanho/32/15191_2.png) [@hollowaykeanho](https://forum.golangbridge.org/u/hollowaykeanho)\
**Post date:** [November 3, 2020, 1:09pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/2 "2020-11-03T13:09:19Z")

</div>

> [@Shantanu\_Shekhar](#):
>
> I was thinking I would have to dereference the pointer like so `*personPtr.age = *personPtr.age + 1` but the compiler complains about this latter option. Why do I not need to dereference the pointer?

Go performs automatic dereferencing for struct data type in its specification. Hence, you do not need to de-reference it explicitly. Quote:

> As with [selectors](https://golang.org/ref/spec#Selectors), a reference to a non-interface method with a value receiver using a pointer will **automatically dereference** that pointer: `pt.Mv` is equivalent to `(*pt).Mv` .
> 
> (More info here: [The Go Programming Language Specification - The Go Programming Language](https://golang.org/ref/spec#Selectors), [The Go Programming Language Specification - The Go Programming Language](https://golang.org/ref/spec#Method_values))

You only need to de-reference slice, map, or array if you’re passing in as a pointer (rare occasion for such deployment but that is another question). Example:

```auto
func myProcess(a *map[string]string) {
     t := (*a)["whatever"]
     ...
}

```

---

<div class="post-metadata">

**Author:** ![Shantanu\_Shekhar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/shantanu_shekhar/32/8665_2.png) [@Shantanu\_Shekhar](https://forum.golangbridge.org/u/Shantanu_Shekhar)\
**Post date:** [November 3, 2020, 1:11pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/3 "2020-11-03T13:11:17Z")

</div>

Thanks a bunch. That is very helpful.

---

<div class="post-metadata">

**Author:** ![vkoster](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/v/97f17d/32.png) [@vkoster](https://forum.golangbridge.org/u/vkoster)\
**Post date:** [January 11, 2021, 2:10pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/4 "2021-01-11T14:10:08Z")

</div>

Hi,  
btw, if you do

```
func happyBirthday(personPtr *person) {
    (*personPtr).age = (*personPtr).age + 1
}

```

it works.

---

<div class="post-metadata">

**Author:** ![petrus](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@petrus](https://forum.golangbridge.org/u/petrus)\
**Post date:** [January 11, 2021, 4:38pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/5 "2021-01-11T16:38:21Z")

</div>

> [@vkoster](#):
>
> ```auto
> func happyBirthday(personPtr *person) {
> (*personPtr).age = (*personPtr).age + 1
> }
> 
> ```
> 
> it works.

Yes, but don’t write it in that complicated way. In Go, we value readability and simplicity. Simply write:

```
func happyBirthday(p *person) {
	p.age++
}

```

> **[Go Playground - The Go Programming Language](https://go.dev/play/p/L_CGyDxa0Fg)**

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [April 11, 2021, 4:38pm UTC](https://forum.golangbridge.org/t/dereferencing-pointer/21201/6 "2021-04-11T16:38:27Z")

</div>

Closed on April 11, 2021, 4:38pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
