[Go] golang x.(type) 用法

前端之家收集整理的这篇文章主要介绍了[Go] golang x.(type) 用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

golang x.(type) 用法
x.(type)只能在switch中使用,和switch case配合使用,获取到类型

 

func MyPrintf(args ...interface{}) {  
    for _,arg := range args {  
        switch arg.(type) {  
            case int:  
                fmt.Println(arg,"is an int value.")  
            case string:case int64:default:)  
        }  
    }  
}

在go-imap中看到使用

        switch h := p.Header.(type) {
        case *mail.InlineHeader:
            // This is the message's text (can be plain-text or HTML)
            b,_ := IoUtil.ReadAll(p.Body)
            log.Println("Got text: ",string(b))
        mail.AttachmentHeader:
             This is an attachment
            filename,_ := h.Filename()
            log.Println("Got attachment: ",1)"> filename)
        }

 

猜你在找的Go相关文章