加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用指针定义golang结构函数

发布时间:2020-12-16 19:20:04 所属栏目:大数据 来源:网络整理
导读:有人向我解释为什么附加到数组的工作原理: func (s *Sample) Append(name string) { d := Stuff{ name: name,} s.data = append(s.data,d)} Full code here 但不是当你这样做: func (s Sample) Append(name string) { d := Stuff{ name: name,d)} 有什么理
有人向我解释为什么附加到数组的工作原理:
func (s *Sample) Append(name string) {
    d := &Stuff{
        name: name,}
    s.data = append(s.data,d)
}

Full code here

但不是当你这样做:

func (s Sample) Append(name string) {
    d := &Stuff{
        name: name,d)
}

有什么理由为什么要使用第二个例子。

如 in the FAQ所述

我应该定义值或指针的方法吗?

func (s *MyStruct) pointerMethod() { } // method on pointer
func (s MyStruct)  valueMethod()   { } // method on value

First,and most important,does the method need to modify the receiver? If it does,the receiver must be a pointer. (Slices and maps act as references,so their story is a little more subtle,but for instance to change the length of a slice in a method the receiver must still be a pointer.)

In the examples above,if pointerMethod modifies the fields of s,the caller will see those changes,but valueMethod is called with a copy of the caller’s argument (that’s the definition of passing a value),so changes it makes will be invisible to the caller.

在你的情况下,func(s Sample)Append(name string)修改一个副本。

laher提醒我们in the comments使用一个值而不是指针也意味着获取一个副本,并尊重对象的不变性::

You’d want to use the non-pointer valueMethod when (for nstance) you’re returning a [value derived from an] ‘immutable’ private property.

参见“Why are receivers pass by value in Go?”:

Can be useful if for instance you have a small immutable object. The caller can know for certain that this method doesn’t modify it’s receiver. They can’t know this if the receiver is a pointer without reading the code first.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读