Last active
January 24, 2022 13:40
-
-
Save donchev7/09136709a446d5558111d684aeac6d73 to your computer and use it in GitHub Desktop.
go without if statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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