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.

29 lines
444 B

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. func doubler(v interface{}) (string, error) {
  7. if i, ok := v.(int); ok {
  8. return fmt.Sprint(i * 2), nil
  9. }
  10. if s, ok := v.(string); ok {
  11. return s + s, nil
  12. }
  13. return "", errors.New("unsupported type passed")
  14. }
  15. func main() {
  16. res, _ := doubler(5)
  17. fmt.Println("5 :", res)
  18. res, _ = doubler("yum")
  19. fmt.Println("yum:", res)
  20. _, err := doubler(true)
  21. fmt.Println("true:", err)
  22. }