Advertisement
Guest User

Golang like with non if-else

a guest
Jan 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.91 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func formatLessThan4(nameList []string) interface{} {
  6.     format := map[int]interface{}{
  7.         0: func(nameList []string) {
  8.             fmt.Printf("no one likes this")
  9.         },
  10.         1: func(nameList []string) {
  11.             fmt.Printf("%s likes this", nameList[0])
  12.         },
  13.         2: func(nameList []string) {
  14.             fmt.Printf("%s and %s like this", nameList[0], nameList[1])
  15.         },
  16.         3: func(nameList []string) {
  17.             fmt.Printf("%s, %s and %s like this", nameList[0], nameList[1], nameList[2])
  18.         },
  19.     }
  20.     return func() {
  21.         format[len(nameList)].(func([]string))(nameList)
  22.     }
  23. }
  24.  
  25. func fb(nameList []string) interface{} {
  26.     formatFunc := map[bool]interface{}{
  27.         true: formatLessThan4(nameList),
  28.         false: func() {
  29.             fmt.Printf("%s, %s and %d others like this", nameList[0], nameList[1], len(nameList)-2)
  30.         },
  31.     }
  32.     return formatFunc[len(nameList) < 4]
  33. }
  34. func main() {
  35.     like := []string{"Peter"}
  36.     fb(like).(func())()
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement