Go map of functions


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' {


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":
        case "g":
            v.(func(string, int))("astring", 42)

Answered By – peterSO

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.