Loop unit char entered

201 views
Skip to first unread message

Daniel Jankins

unread,
Apr 15, 2022, 9:35:44 AM4/15/22
to golan...@googlegroups.com
Hi All,
I have a newbie question.  I want to loop until a char is available (keypress).
Could someone point me to an example.
 Thank you

--
DanJ

Kurtis Rader

unread,
Apr 15, 2022, 8:48:35 PM4/15/22
to Daniel Jankins, golang-nuts
On Fri, Apr 15, 2022 at 6:35 AM Daniel Jankins <djan...@gmail.com> wrote:
I have a newbie question.  I want to loop until a char is available (keypress).
Could someone point me to an example.

I can't point you to an example because that wouldn't be idiomatic Go; thus such examples are (I hope) hard to find. In Go you shouldn't busy wait using a function like "kbhit" . You should create a channel for communicating the keys that are seen, create a goroutine that reads from os.Stdin and injects keys into that channel, then use an event loop in a different goroutine that uses "select" to read key events from that channel.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Daniel Jankins

unread,
Apr 16, 2022, 6:21:30 AM4/16/22
to Kurtis Rader, golang-nuts
Thank you,
That is what I did.  It works very well.
Thank you again
--
DanJ

Michael Adamus

unread,
Mar 5, 2024, 3:35:09 PM3/5/24
to golang-nuts
Daniel,
I'd like to see source code for this if you're able.

Thanks,
Mike

Reply all
Reply to author
Forward
0 new messages