Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
536 B

  1. package main
  2. import "fmt"
  3. func getData() []interface{} {
  4. return []interface{}{
  5. 1,
  6. 3.14,
  7. "hello",
  8. true,
  9. struct{}{},
  10. }
  11. }
  12. func typeCheck(v interface{}) string {
  13. switch v.(type) {
  14. case string:
  15. return "string"
  16. case bool:
  17. return "bool"
  18. case float32, float64:
  19. return "float"
  20. case int, int32, int64:
  21. return "int"
  22. default:
  23. return "unknown"
  24. }
  25. }
  26. func main() {
  27. data := getData()
  28. for i := 0; i < len(data); i++ {
  29. fmt.Printf("%v is %v\n", data[i], typeCheck(data[i]))
  30. }
  31. }