I want to pass a pointer to something into a function, without knowing its type at compile time, have the function write to it. Here's what I thought would work:
func foo(dest interface{}) {
switch (dest).(type) {
case *int:
fmt.Println("got int")
*dest = 1
// handle other cases...
}
}
However, calling this with an *int
input
func main() {
bar := 2
foo(&bar)
fmt.Println(bar) // expect 1
}
yields the compiler error
invalid indirect of dest (type interface {})
.
What am I doing wrong here?