dongxingchang9345 2014-06-24 15:48
浏览 77
已采纳

指针接收器和值接收器在Golang中是什么意思?

I've been getting errors from go saying stuff about pointer receivers and I decided to google what the terms mean and I read different sources and documentation talking about pointer receivers. For example: http://golang.org/doc/faq and http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go.

Though, eventhough they talk about these terms they failed to define them precisely. Though, from the context I think the difference between them are defining variables as pointers like *MyStruct vs MyStruct. Although, I am not 100% sure of their difference, I wanted to get a more official or solid understanding of the terms, specially their difference (pointer receiver and value receiver). If possible some simple example code showing their difference in go would be awesome! (and probably necessary to really understand this)

Like for example, something that is confusing me is, what is the difference between the term pointer and pointer receiver? or Value and value receiver? What does the term receiver add to these concepts?

  • 写回答

1条回答 默认 最新

  • dqyin0101 2014-06-24 16:09
    关注

    Since you clarified you're confused by the term receiver and not the pointer/value distinction. In Go "receiver" refers to the value a method is defined on, for purposes of interfaces. You can think of the receiver as a special case of the first argument to a function.

    func (m MyStruct) DoStuff()
    

    This is what's known as a "value receiver", it is defined on the value MyStruct. This is functionally identical to:

    func DoStuff(m MyStruct)
    

    Except:

    With a "receiver" you call the function with ".", like in many OO languages:

     m := MyStruct{} 
     m.DoStuff() // as opposed to DoStuff(m)
    

    The set of methods a type is a receiver on defines the interface it implements:

    type DoesStuff interface {
        DoStuff()
    }
    
    func DoSomething(d DoesStuff) {
        d.DoStuff()
    }
    
    func main() {
        m := MyStruct{}
        DoSomething(m)
    }
    

    So what's a pointer receiver? It looks like this:

    func (m *MyStruct) DoOtherStuff()
    

    The difference is exactly the difference between a pointer and a value. Though minor semantic changes occur. Go will auto address and auto-dereference pointers (in most cases) so m := MyStruct{}; m.DoOtherStuff() still works since Go automatically does (&m).DoOtherStuff() for you. (Naturally, you're free to do m := &MyStruct{}; m.DoOtherStuff as well). Further, the interface is defined on the pointer, so:

    type DoesOtherStuff interface {
        DoOtherStuff()
    }
    
    func DoSomethingElse(d DoesOtherStuff) {
        d.DoOtherStuff()
    }
    
    func main() {
        m := MyStruct{}
        // DoSomethingElse(m) will fail since because the interface
        // DoesOtherStuff is defined on a pointer receiver and this is a value
        DoSomethingElse(&m)
    }
    

    If you're still confused about when to use a pointer receiver versus a variable receiver, the short answer is: probably a pointer receiver. The long answer has been answered several times, but I'll link here simply because it was easy to find in my history.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记