首页 > 其他 > 详细

[Go] golang x.(type) 用法

时间:2020-05-14 19:46:28      阅读:81      评论:0      收藏:0      [点我收藏+]

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:  
                fmt.Println(arg, "is a string value.")  
            case int64:  
                fmt.Println(arg, "is an int64 value.")  
            default:  
                fmt.Println(arg, "is an unknown type.")  
        }  
    }  
}

在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))
        case *mail.AttachmentHeader:
            // This is an attachment
            filename, _ := h.Filename()
            log.Println("Got attachment: ", filename)
        }

 

[Go] golang x.(type) 用法

原文:https://www.cnblogs.com/taoshihan/p/12890748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!