编译失败 (goroutine 导致)
初学go,尝试用robotgo包捕捉键盘输入,一直编译不通过,可能是对goroutine理解有问题,还请前辈指点迷津.
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
)
type word struct {
key string
desc string
}
func keybord(action chan string, words []word, quit chan int) {
for _, v := range words {
go func(w word) {
// fmt.Println("go func param")
// fmt.Println(word)
if event := robotgo.AddEvent(w.key); event == true {
// fmt.Println("event")
// fmt.Println(w.key)
if (w.desc == "bye") {
quit <- 0
}
action <- w.desc
}
}(v)
}
}
func main() {
action := make(chan string)
quit := make(chan int)
fmt.Println("监控键盘输入")
if event := robotgo.AddEvent("down"); event == true {
fmt.Println("down")
}
words := []word{
{"right", "向右"},
{"left", "向左"},
{"up", "向上"},
{"down", "向下"},
{"esc", "bye"},
}
keybord(action, words, quit)
for {
select {
case tmp := <-action:
fmt.Println("main")
fmt.Println(tmp)
case <-quit:
fmt.Println("main")
fmt.Println("quit")
return
}
}
}
关键报错信息:
...
SIGABRT: abort
PC=0x7fffcb415dda m=4 sigcode=0
goroutine 0 [idle]:
runtime: unknown pc 0x7fffcb415dda
stack: frame={sp:0x70000e5c5ce8, fp:0x0} stack=[0x70000e546290,0x70000e5c5e90)
000070000e5c5be8: 00007fffcb4f3d65 0000000000000001
...
谢谢.