我想将指向某个东西的指针传递给一个函数,而在编译时不知道它的类型,让函数写入它。这是我认为可行的方法:
func foo(dest interface{}) {
switch (dest).(type) {
case *int:
fmt.Println("got int")
*dest = 1
// handle other cases...
}
}
但是,使用*int输入调用它
func main() {
bar := 2
foo(&bar)
fmt.Println(bar) // expect 1
}
产生编译器错误
invalid indirect of dest (type interface {}).
我在这里做错了什么?
神不在的星期二
杨__羊羊
富国沪深
相关分类