func funcName(a interface{}) string {
return string(a)
}
就会报错:cannot convert a (type interface{}) to type string: need type assertion 不能直接将interface{}类型转为string,需要断言
func Test(a interface{}) string {
value, ok := a.(string)
if !ok {
fmt.Println("It's not ok for type string",value)
return
}
return value
}
func main(){
a:="abcd"
c1:=Test(a)
b:=123
c2:=Test(b)
fmt.Println(c1,c2)
}