I have a Go function that has interface{} as a parameter. When I call the function with string, it will cast one alloc/ns. Why?
func foo(...interface{}) error {
....
}
func use() {
var str = "use it"
e := foo(str)
_ = e
}
Internally, an interface variable is a two word structure. The first word is a pointer to the information about the dynamic type of the variable. The second word will either (a) contain the variable's dynamic value if it will fit in a word, or (b) contain a pointer to memory holding the dynamic value if it is larger.
A string variable is larger than a word, since it holds both it's length and a pointer to the underlying character data. So storing a string in a an interface variable involves allocating some memory to hold that value.