Skip to content

Instantly share code, notes, and snippets.

@bmatsuo
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatsuo/1e30d002faf2bbe26dae to your computer and use it in GitHub Desktop.
Save bmatsuo/1e30d002faf2bbe26dae to your computer and use it in GitHub Desktop.
// The trouble with specialized map types... I need a better name.
package main
import "fmt"
type I interface {
M()
}
type IFunc func()
func (fn IFunc) M() {
fn()
}
func printi(s string, impl I) {
fmt.Println(s)
impl.M()
}
func map1(m map[string]I) {
for s, impl := range m {
printi(s, impl)
}
}
func map2(m map[string]IFunc) {
for s, fn := range m {
printi(s, fn)
}
}
func map3(m Map) {
for s, fn := range m {
printi(s, fn)
}
}
type Map map[string]I
type MapFunc map[string]IFunc
type Map2 map[string]I
func main() {
map1(Map{"ok": IFunc(func() { fmt.Println("boom") })})
map2(MapFunc{"ok": func() { fmt.Println("boom") }})
map3(map[string]I{"ok": IFunc(func() { fmt.Println("boom") })})
//map3(Map2{"ok": IFunc(func() { fmt.Println("boom") })}) // compilation error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment