Go map of functions

55,532

Solution 1

Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters.

package main

import "fmt"

func f(p string) {
    fmt.Println("function f parameter:", p)
}

func g(p string, q int) {
    fmt.Println("function g parameters:", p, q)
}

func main() {
    m := map[string]interface{}{
        "f": f,
        "g": g,
    }
    for k, v := range m {
        switch k {
        case "f":
            v.(func(string))("astring")
        case "g":
            v.(func(string, int))("astring", 42)
        }
    }
}

Solution 2

m := map[string]func(string, string)

Works if you know the signature (and all the funcs have the same signature) I think this is cleaner/safer than using interface{}

Solution 3

You can define a type if functions are same interface.

package main

import "log"

type fn func (string)

func foo(msg string) {
  log.Printf("foo! Message is %s", msg)
}

func bar(msg string) {
  log.Printf("bar! Message is %s", msg)
}

func main() {
  m := map[string] fn {
    "f": foo,
    "b": bar,
  }
  log.Printf("map is %v", m)
  m["f"]("Hello")
  m["b"]("World")
}

Solution 4

@Seth Hoenig's answer helped me best, but I just wanted to add that Go accepts functions with defined return value as well:

package main

func main() {
    m := map[string]func(string) string{
        "foo": func(s string) string { return s + "nurf" },
    }

    m["foo"]("baz") // "baznurf"
}

If you think it's ugly, you could always use a type (see @smagch's answer).

Solution 5

I used a map[string]func (a type, b *type) I passed a string to search the map and a pointer to modify the slice.

Hope that helps!

var Exceptions map[string]func(step string, item *structs.Item)

func SetExceptions() {
    Exceptions = map[string]func(a string, i *structs.Item){
        "step1": step1,
    }
}

func RunExceptions(state string, item *structs.Item) {
    method, methBool := Exceptions[state]
    if methBool {
        method(state, item)
    }
}

func step1(step string, item *structs.Item) {
    item.Title = "Modified"
}
Share:
55,532
Conceited Code
Author by

Conceited Code

Updated on May 29, 2020

Comments

  • Conceited Code
    Conceited Code about 4 years

    I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that?

    I have tried this, but this doesn't work.

    func a(param string) {
    
    }
    
    m := map[string] func {
        'a_func': a,
    }
    
    for key, value := range m {
       if key == 'a_func' {
        value(param) 
       }
    }
    
  • Conceited Code
    Conceited Code almost 13 years
    that's almost exactly what I need, but let's say g needed 2 string parameters. Is that possible?
  • peterSO
    peterSO almost 13 years
    All the functions don't have the same signature.
  • Docunext
    Docunext over 11 years
    Could you elaborate on "v.(func(string))"; that seems like it would almost need the reflect package. Do the parens here do any casting, or is it all dealing with the basic interface?
  • Docunext
    Docunext over 11 years
    And instead of using f and g as the map values, could you instead have string values there, and instead call f(mapValue) under case "f":?
  • Docunext
    Docunext over 11 years
    Actually I think this answers my above question; my functions have the same signature. But... here I see the map value is a function with arguments. Interesting, I'll try that as well as passing just the arguments to compare.
  • GreenRaccoon23
    GreenRaccoon23 about 9 years
    Also, say the function returns a string variable, it would look like this: m := map[string]func(string, string)string
  • Admin
    Admin almost 4 years
    How to use a type: play.golang.org/p/CtVoOzkT3hw. Good example +1.
  • Chuque
    Chuque about 3 years
    what if your functions are from a struct type?