1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package main
import ( "fmt" )
type Handler interface { Do(k, v interface{}) }
type HandlerFunc func(k, v interface{})
func (f HandlerFunc) Do(k, v interface{}) { f(k, v) }
func Each(m map[interface{}]interface{}, h Handler) { if m != nil && len(m) > 0 { for k, v := range m { h.Do(k, v) } } }
func EachFunc(m map[interface{}]interface{}, f func(k, v interface{})) { Each(m, HandlerFunc(f)) }
func selfInfo(k, v interface{}) { fmt.Printf("大家好,我叫%s,今年%d岁\n", k, v) }
func main() { persons := make(map[interface{}]interface{}) persons["张三"] = 20 persons["李四"] = 23 persons["王五"] = 26
EachFunc(persons, selfInfo)
}
|