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)
}
来源:oschina
链接:https://my.oschina.net/u/4261498/blog/4279387