Assumptions: In go, all function arguments are passed by value. To get pass-by-reference semantics/performance, go programmers pass values by pointer. Go will still make a copy of these arguments, but its making a copy of a pointer which is sometimes more memory efficient than making a copy of the actual parameter.
Question: What is going on when you pass an interface? i.e., in a program like this
package main
import "fmt"
type Messages struct {
hello string
}
func main() {
sayHelloOne(Messages{"hello world"});
sayHelloTwo(&Messages{"hello world"});
sayHelloThree(Messages{"hello world"});
}
//go makes a copy of the struct
func sayHelloOne(messages Messages) {
fmt.Println(messages.hello)
}
//go makes a *pointer* to the struct
func sayHelloTwo(messages *Messages) {
fmt.Println(messages.hello)
}
//go --- ???
func sayHelloThree(messages interface{}) {
fmt.Println(messages.(Messages).hello)
}
What happens with the argument when a programmer calls the sayHelloThree
function? Is messages
being copied? Or is it a pointer to messages
that's copied? Or is there some weird-to-me deferring going on until messages
is cast?