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

指针接收器和值接收器在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?

图片转代码服务由CSDN问答提供 功能建议

我一直在说一些关于指针接收器的错误,因此我决定在Google上搜索这些术语的含义,我 阅读有关指针接收器的不同资源和文档。 例如: http://golang.org/doc/faq http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go

尽管,尽管他们谈论这些术语,但仍未能准确定义它们。 不过,从上下文来看,我认为它们之间的区别在于将变量定义为类似 * MyStruct MyStruct 的指针。 尽管我不是100%肯定它们之间的区别,但是我想对这些术语,尤其是它们的区别(指针接收者和值接收者)有一个更正式或扎实的理解。 如果可能的话,一些简单的示例代码将显示它们在运行中的不同之处,那就太棒了! (也许是真正理解这一点所必需的)

例如,令我感到困惑的是,术语“指针”和“指针接收器”之间有什么区别? 还是价值和价值接收者? 接收者一词对这些概念有什么补充?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqyin0101
    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.

    点赞 评论

相关推荐