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}