Skip to content

Instantly share code, notes, and snippets.

@eiyaya
Last active January 1, 2016 12:09
Show Gist options
  • Save eiyaya/8142447 to your computer and use it in GitHub Desktop.
Save eiyaya/8142447 to your computer and use it in GitHub Desktop.
O(1) cmd parser
package main
import "fmt"
type CMD interface {
doAction(user string)
}
type EAT struct {
}
type LOOK struct {
}
func (look *LOOK) doAction(user string) {
fmt.Println(user, "are looking")
}
func (eat *EAT) doAction(user string) {
fmt.Println(user, "are eating")
}
func main() {
var cmds [10]CMD
cmds[0] = &EAT{}
cmds[1] = &LOOK{}
call(cmds[0], "junjun")
call(cmds[1], "stephen")
}
func call(cmd CMD, user string) {
cmd.doAction(user)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment