Go map of functions

MapGoKey ValueFunc

Map Problem Overview


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) } }

Map Solutions


Solution 1 - Map

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 - Map

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 - Map

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 - Map

@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 - Map

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"
}

Solution 6 - Map

Here is the way I made it work in my case:

package main

import (
	"fmt"
)

var routes map[string]func() string

func main() {
	routes = map[string]func() string{
		"GET /":      homePage,
		"GET /about": aboutPage,
	}

	fmt.Println("GET /", pageContent("GET /"))
	fmt.Println("GET /about", pageContent("GET /about"))
	fmt.Println("GET /unknown", pageContent("GET /unknown"))
    // Output:
    // GET / Home page
    // GET /about About page
    // GET /unknown 404: Page Not Found
}

func pageContent(route string) string {
	page, ok := routes[route]
	if ok {
		return page()
	} else {
		return notFoundPage()
	}
}

func homePage() string {
	return "Home page"
}

func aboutPage() string {
	return "About page"
}

func notFoundPage() string {
	return "404: Page Not Found"
}

https://play.golang.org/p/8_g6Di1OKZS

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionConceited CodeView Question on Stackoverflow
Solution 1 - MappeterSOView Answer on Stackoverflow
Solution 2 - MapSeth HoenigView Answer on Stackoverflow
Solution 3 - MapsmagchView Answer on Stackoverflow
Solution 4 - MaptlebView Answer on Stackoverflow
Solution 5 - Maphugo salinasView Answer on Stackoverflow
Solution 6 - Mapck3gView Answer on Stackoverflow