Skip to content

Instantly share code, notes, and snippets.

@NewbMiao
Last active November 1, 2017 06:40
Show Gist options
  • Save NewbMiao/6159c78af490903850d3c601bb27d39a to your computer and use it in GitHub Desktop.
Save NewbMiao/6159c78af490903850d3c601bb27d39a to your computer and use it in GitHub Desktop.
go interface func
//http://www.flysnow.org/2016/12/30/golang-function-interface.html
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment