Golang use of .(type) outside type switch 关于参数类型
发布时间:2020-12-16 18:04:55 所属栏目:大数据 来源:网络整理
导读:Golang 中如何获取参数的类型? 执行使用以下语句: fmt.Println( "type:" ,v.( type )) 提示错误: use of .(type) outside type switch 正确的使用方法是必须在switch case中。 举例如下: package main import ( "fmt" ) func main() { CheckType( "tow"
Golang 中如何获取参数的类型? fmt.Println("type:",v.(type))
提示错误:
正确的使用方法是必须在switch case中。 package main
import (
"fmt"
)
func main() {
CheckType("tow", 88,"three")
}
func CheckType(args ...interface{}) {
for _,v := range args {
switch v.(type) {
case int:
fmt.Println("type:int,value:",v)
case string:
fmt.Println("type:string,v)
default:
fmt.Println("type:unkown,v)
}
}
}
编译,执行 $ go build arm.go
$ ./arm
结果:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |