Skip to content

Instantly share code, notes, and snippets.

@donchev7
Last active January 24, 2022 13:40
Show Gist options
  • Save donchev7/09136709a446d5558111d684aeac6d73 to your computer and use it in GitHub Desktop.
Save donchev7/09136709a446d5558111d684aeac6d73 to your computer and use it in GitHub Desktop.
go without if statements
package main
import (
fmt
)
func main() {
type fnType = func(a int, b int) int
fnMap := map[string]fnType{
"ADD": func(a int, b int) int {
return a + b
},
"SUB": func(a int, b int) int {
return a - b
},
}
// closure
localFn := func(method string) fnType {
return fnMap[method] // returns a function
}
printer := func(fn func(method string) fnType, method string) {
fmt.Println(fn(method)(10, 5)) // callback
}
// Victory
printer(localFn, "ADD")
printer(localFn, "SUB")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment