【问题标题】:Set pointer to nil from method or function从方法或函数将指针设置为 nil
【发布时间】:2019-02-16 19:37:36
【问题描述】:

为什么这两个destroy 函数都没有将指针更改为 nil,我该如何创建这样的函数?

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)
    }

}

输出:

&{1 1}
&{2 2}

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

【问题讨论】:

标签: go


【解决方案1】:

您需要pointer to pointer 来更改指针的值。

这是您的代码示例,已修改为执行此操作 (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)
    }
}

在您当前的代码中

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

destroy 内部,p 是一个保存position 结构地址的值。通过给p 本身分配一些东西,你只是让它拥有一些其他position 结构(或nil)的地址。您没有修改传入的原始指针。

这与试图通过赋值来修改其参数的函数没有什么不同:

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

Go 规范在 section about calls and call parameters 中说:

它们被评估之后,调用的参数被传递 函数的值和被调用的函数开始执行。这 函数的返回参数按值传回 函数返回时调用函数。

【讨论】:

  • 注意go的传值行为在CallsCalls下的规范中定义
  • @PaSTE:感谢您的建议 - 我已将其纳入答案(下次请随时提出修改建议!)
猜你喜欢
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
  • 2013-10-31
  • 2018-01-05
  • 2010-12-29
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多