加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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))

提示错误:

use of .(type) outside type switch

正确的使用方法是必须在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

结果:

type:string,value: tow type:int,value: 88 type:string,value: three

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读