[英]Is a type that extends a Map still passed by reference in Go?
If you have a type in Go, that extends a Map, is this new type still "passed by reference" (or respectively passed as a pointer value)?如果您在 Go 中有一个扩展 Map 的类型,这种新类型是否仍然“通过引用传递”(或分别作为指针值传递)? Or is it copied when used as a function parameter?
还是在用作 function 参数时被复制?
Example:例子:
type myMapType map[string][]int
func doSomething(m myMapType) myMapType{
// do something
return m
}
I want to prevent unnecessary copying of data.我想防止不必要的数据复制。 So do I need a pointer for the above example, or not?
那么我是否需要上述示例的指针?
Thanks in advance!提前致谢!
When you declare当你声明
type myMapType map[string][]int
You now have a type called myMapType
, whose behaviour is the same as map[string][]int
.您现在有一个名为
myMapType
的类型,其行为与map[string][]int
相同。
It's the same as if every instance of myMapType
was an instance of map[string][]int
, with the only difference being that their types are not considered "the same" in cases where that matters (assignment, type switch, type assertion, etc.).就像
myMapType
的每个实例都是map[string][]int
的实例一样,唯一的区别是它们的类型在重要的情况下不被认为是“相同的”(赋值、类型切换、类型断言、 ETC。)。
In other words, myMapType
and map[string][]int
are equi-valent but not identic-al .换句话说,
myMapType
和map[string][]int
是等价的,但不是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.