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 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口