emptyint_switch.go 627 B

123456789101112131415161718192021222324252627282930313233
  1. package main
  2. import "fmt"
  3. type specialString string
  4. var whatIsThis specialString = "hello"
  5. func TypeSwitch() {
  6. testFunc := func(any interface{}) {
  7. switch v := any.(type) {
  8. case bool:
  9. fmt.Printf("any %v is a bool type", v)
  10. case int:
  11. fmt.Printf("any %v is an int type", v)
  12. case float32:
  13. fmt.Printf("any %v is a float32 type", v)
  14. case string:
  15. fmt.Printf("any %v is a string type", v)
  16. case specialString:
  17. fmt.Printf("any %v is a special String!", v)
  18. default:
  19. fmt.Println("unknown type!")
  20. }
  21. }
  22. testFunc(whatIsThis)
  23. }
  24. func main() {
  25. TypeSwitch()
  26. }
  27. // Output: any hello is a special String!