Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Last active October 9, 2020 13:33
Show Gist options
  • Save FerdinaKusumah/0722a1452ee8da2f9dd4979fa16bebe3 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/0722a1452ee8da2f9dd4979fa16bebe3 to your computer and use it in GitHub Desktop.
Simple Condition
package main
import "fmt"
func addNumber(x, y int) int {
return x + y
}
func subtractNumber(x, y int) int {
return x - y
}
func divideNumber(x, y int) int {
return x / y
}
func multiplyNumber(x, y int) int {
return x * y
}
var FilterMap = map[string]interface{}{
"add": addNumber,
"sub": subtractNumber,
"mul": multiplyNumber,
"div": divideNumber,
}
func calculateNumber(x, y int, param string) int {
// since go can't assign action value if lookup table is not found
// so we need to check if keys is exists in hash map
if cal, ok := FilterMap[param]; ok {
return cal.(func(int, int) int)(x, y)
}
return 0
}
func main() {
var x, y = 10, 2
fmt.Println(calculateNumber(x, y, "div"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment