type assertion

大象笔记 > 标签 > type assertion

cannot use variable (type interface {}) as type int in assignment: need type assertion

在使用 golang gin 时,通过 context get 获取的值在赋值给一个整型变量时,报错 cannot use variable (type interface {}) as type int in assignment: need type assertion 代码模拟如下: package main import "fmt" func main() { var tmp interface{} var i int tmp = 1 i = tmp fmt.Println(i) } 报错的代码行是 i = tmp 那行。 可见,interfac ...

阅读全文...