/** * 使用匿名函數實現操做封裝 * 將匿名函數做爲 map 的鍵值,經過命令行參數動態調用匿名函數 * 運行命令 go run src/hh/main.go --skill=fly */ package main import ( "flag" "fmt" ) var skillParam = flag.String("skill", "", "skill to perform") func main() { flag.Parse() var skill = map[string]func(){ "fire": func() { fmt.Println("chicken fire") }, "run": func() { fmt.Println("soldier run") }, "fly": func() { fmt.Println("angel fly") }, } if f, ok := skill[*skillParam]; ok { f() } else { fmt.Println("skill not found") } }