I have the following function:
func addCatsToMap(m map[string][]CatHouse, meowId int, treats Set, dog *Dog) {
//if (complicated thing) add Cat to m
}
where Set, the type of treats, is an interface with the following definition:
type Set interface {
Add(value string)
Contains(value string) (bool)
Length() (int)
RemoveDuplicates()
}
Question:
Is it true that m, treats, and dog are passed-by-reference, and meowId has it’s value copied?
I assume that:
mis pass-by-reference because it’s a mapdogis a struct. So, I should pass the pointer to avoid copying the data
An interface type is simply a set of methods. Notice that the members of an interface definition do not specify whether or not the receiver type is a pointer. That is because the method set of a value type is a subset of the method set of its associated pointer type. That’s a mouthful. What I mean is, if you have the following:
and you define the following two methods:
Then the type
Whateverhas only the methodBar(), while the type*Whateverhas the methodsFoo()andBar(). That means if you have the following interface:Then
*WhateverimplementsGritsbutWhateverdoes not, becauseWhateverlacks the methodFoo(). When you define the input to a function as an interface type, you have no idea whether it’s a pointer or a value type.The following example illustrates a function that takes an interface type in both ways:
you could call
Raname(&f, "Jorelli Fruit")but notRename(c, "Jorelli Bar"), because bothFruitand*FruitimplementRenamable, while*CandyimplementsRenableandCandydoes not.http://play.golang.org/p/Fb-L8Bvuwj