Last active
January 1, 2016 12:09
-
-
Save eiyaya/8142447 to your computer and use it in GitHub Desktop.
O(1) cmd parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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