package main
import (
"fmt"
"time"
)
func main() {
i :=2
fmt.Println("Write", i ,"as")
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
switch time.Now().Weekday() {
case time.Saturday,time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("Its a weekday")
}
//理解是獲取接口實例實際的類型指針,以此調用實例所有可調用的方法,包括接口方法及自有方法。
//需要注意的是該寫法必須與switch case聯合使用,case中列出實現該接口的類型。
//interface{}是可以傳任意參數的,i.(type)必須在switch語句里使用,如果需要在其他位置就要使用reflect.Typeof
whatAmI := func(i interface{}) {
switch t:=i.(type) {
case bool:
fmt.Println("bool")
case int:
fmt.Println("int")
default:
fmt.Printf("dont know type %T",t)
}
}
whatAmI((true))
whatAmI((1))
whatAmI("hey")
}