4 回答

TA貢獻1898條經驗 獲得超8個贊
我也在為 i3 中的點擊事件編寫自己的處理程序。這就是我偶然發現這個線程的方式。
Golang 標準庫實際上確實完成了所需的工作(Golang 1.12)。不確定當你問這個問題的時候有沒有?
// json parser read from stdin
decoder := json.NewDecoder(os.Stdin)
// Get he first token - it should be a '['
tk, err := decoder.Token()
if err != nil {
fmt.Fprintln(os.Stderr, "couldn't read from stdin")
return
}
// according to the docs a json.Delim is one [, ], { or }
// Make sure the token is a delim
delim, ok := tk.(json.Delim)
if !ok {
fmt.Fprintln(os.Stderr, "first token not a delim")
return
}
// Make sure the value of the delim is '['
if delim != json.Delim('[') {
fmt.Fprintln(os.Stderr, "first token not a [")
return
}
// The parser took the [ above
// therefore decoder.More() will return
// true until a closing ] is seen - i.e never
for decoder.More() {
// make a Click struct with the relevant json structure tags
click := &Click{}
// This will block until we have a complete JSON object
// on stdin
err = decoder.Decode(click)
if err != nil {
fmt.Fprintln(os.Stderr, "couldn't decode click")
return
}
// Do something with click event
}
- 4 回答
- 0 關注
- 231 瀏覽
添加回答
舉報