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