title: map with function pointer as key in go
body: <p>I recently used a map in one of my golang projects, that had function pointers as keys like this:</p>
  
  <pre><code>map[*functiontype] somestructtype
  </code></pre>
  
  <p>One of my colleagues said this was a bad idea, so now I am unsure of this being feasible. I initially deemed it ok, because method pointers can be checked for equality and are immutable. Can someone provide some reasoning on that matter?</p>
  
  <p>Complete example:</p>
  
  <pre><code>package main
  
  import "fmt"
  
  type s struct {
      string
  }
  
  type f func() string
  func func1() string { return "func 1" }
  func func2() string { return "func 2" }
  
  func main() {
      // make two functions and two pointers to them
      f1, f2 := func1, func2
      p1, p2 := (*f)(&amp;f1), (*f)(&amp;f2)
  
      // make a map of their function pointers
      m := make(map[*f]s)
      m[p1] = s{"struct 1"}
      m[p2] = s{"struct 2"}
  
      // print out the mapping
      printmapping(m, p1, p2)
  
      // reverse the pointers and have that printed
      p1, p2 = (*f)(&amp;f2), (*f)(&amp;f1)
      printmapping(m, p1, p2)
  }
  
  func printmapping(m map[*f]s, p1, p2 *f) {
      fmt.Println("pointer 1:", m[(*f)(p1)])
      fmt.Println("pointer 2:", m[(*f)(p2)])
  }
  </code></pre>