Question: Is there a way to convert a string (e.g. "+", "-", "/", "\*") into their respective math operators (+, -, /, \*)?

In Python you can do:

```
import operator
ops = {"+": operator.add, "-": operator.sub} # etc.
print ops["+"](1,1) # prints 2

```

Is there a similar library or method for Go?

Answer: 
You can do this with function values:

```
ops := map[string]func(int, int) int{
    "+": func(a, b int) int { return a + b },
    "-": func(a, b int) int { return a - b },
    "*": func(a, b int) int { return a * b },
    "/": func(a, b int) int { return a / b },
}

fmt.Println(ops["+"](4, 2))
fmt.Println(ops["-"](4, 2))
fmt.Println(ops["*"](4, 2))
fmt.Println(ops["/"](4, 2))

```

Output: [Go Playground](http://play.golang.org/p/eJWoaayfqt)

```
6
2
8
2

```

For a nice print:

```
a, b := 4, 2
for op, fv := range ops {
    fmt.Printf("%d %s %d = %d\n", a, op, b, fv(a, b))
}

```

Output:

```
4 / 2 = 2
4 + 2 = 6
4 - 2 = 2
4 * 2 = 8

```
Here's another implementation. This is give or take 3x faster than the string-based switch implementation but readability is a little less.

```
func RunOp(sign string, a, b int) int {
    s := byte(sign[0])
    switch s {
    case byte(43):
            return a+b
    case byte(45):
            return a-b
    case byte(47):
            return a/b
    case byte(42):
            return a*b
    default:
            return 0
    }
}

```