2

Why both these destroy functions do not change pointer to nil and how can I create such function?

package main

import (
    "fmt"
)

type position struct {
    x int
    y int
}

func (p *position) destroy() {
    p = nil
}

func destroy(p *position) {
    p = nil
}

func main() {
    p1 := &position{1,1}
    p2 := &position{2,2}
    p1.destroy()
    destroy(p2)

    if p1 == nil {
        fmt.Println("p1 == nil")
    } else {
        fmt.Println(p1)
    }

    if p2 == nil {
        fmt.Println("p2 == nil")
    } else {
        fmt.Println(p2)
    }

}

Outputs:

&{1 1}
&{2 2}

https://play.golang.org/p/BmZjX1Hw24u

Marcin Doliwa
  • 3,639
  • 3
  • 37
  • 62
  • 1
    See related / possible duplicates: [What use case does pointers to pointer (eg **int) have?](https://stackoverflow.com/questions/45288751/what-use-case-does-pointers-to-pointer-eg-int-have/45297016#45297016); and [Golang: Can the pointer in a struct pointer method be reassigned to another instance?](https://stackoverflow.com/questions/35421495/golang-can-the-pointer-in-a-struct-pointer-method-be-reassigned-to-another-inst/35426997#35426997) – icza Feb 16 '19 at 19:52

1 Answers1

6

You need a pointer to pointer to change a pointer's value.

Here's your code sample, modified to do this (playground):

package main

import (
    "fmt"
)

type position struct {
    x int
    y int
}

func destroy(p **position) {
    *p = nil
}

func main() {
    p1 := &position{1, 1}
    destroy(&p1)

    if p1 == nil {
        fmt.Println("p1 == nil")
    } else {
        fmt.Println(p1)
    }
}

In your current code

func destroy(p *position) {
    p = nil
}

Inside destroy, p is a value the holds the address of a position struct. By assigning something to p itself, you're simply making it hold the address of some other position struct (or nil). You're not modifying the original pointer passed in.

This isn't different from a function trying to modify its argument by assigning to it:

// This will not actually modify the argument passed in by the caller
func setto2(value int) {
  value = 2
}

The go spec says, in the section about calls and call parameters:

After they are evaluated, the parameters of the call are passed by value to the function and the called function begins execution. The return parameters of the function are passed by value back to the calling function when the function returns.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    Note that the pass-by-value behavior of go is defined in the spec under [Calls](https://golang.org/ref/spec#Calls) – PaSTE Feb 16 '19 at 19:48
  • @PaSTE: thanks for the suggestion - I have worked this into the answer (feel free to suggest an edit next time!) – Eli Bendersky Feb 16 '19 at 19:50