我有以下功能:
其中
题:
是
我认为:
-
m 是引用传递的,因为它是一个映射 -
dog 是一个结构。 因此,我应该传递指针以避免复制数据
接口类型只是一组方法。请注意,接口定义的成员未指定接收方类型是否为指针。这是因为值类型的方法集是其关联的指针类型的方法集的子集。满嘴我的意思是,如果您具有以下条件:
然后定义以下两种方法:
然后,类型
然后
下面的示例说明了一种采用两种方式都采用接口类型的函数:
您可以调用
http://play.golang.org/p/Fb-L8Bvuwj
引用传递是一种语言,Go中没有什么是"引用传递"。通过引用传递意味着赋值运算符可以在单独使用时更改原始值。但是,有些引用类型(例如映射和指针)指向某处。除非您使用其他操作符,例如地图索引和
您正确的认为映射
如果存在真实的"通过引用传递",第二个示例将修改原始地图。
与使用
尽管您不能修改传递的原始接口,但该接口可以包含指针类型。在这种情况下,它的作用就像狗指针,其中某些方法的调用可以修改原始方法。对于您的特定
Calls, The Go Programming Language Specification
In a function call, the function value and arguments are evaluated in
the usual order. After they are evaluated, the parameters of the call
are passed by value to the function and the called function begins
execution. The return parameters of the function are passed by value
back to the calling function when the function returns.When are function parameters passed by value? FAQ - The Go
Programming Language.As in all languages in the C family,
everything in Go is passed by value. That is, a function always gets a
copy of the thing being passed, as if there were an assignment
statement assigning the value to the parameter. For instance, passing
an int value to a function makes a copy of the int, and passing a
pointer value makes a copy of the pointer, but not the data it points
to. (See the next section for a discussion of how this affects method
receivers.)Map and slice values behave like pointers: they are descriptors that
contain pointers to the underlying map or slice data. Copying a map or
slice value doesn't copy the data it points to. Copying an interface
value makes a copy of the thing stored in the interface value. If the
interface value holds a struct, copying the interface value makes a
copy of the struct. If the interface value holds a pointer, copying
the interface value makes a copy of the pointer, but again not the
data it points to.