How to scan input with termui?

356 views
Skip to first unread message

洪嘉鴻

unread,
Apr 16, 2020, 3:46:38 AM4/16/20
to golang-nuts
Hello everyone:
I use golang with Win10. I started to learn termui recently.
However, I couldn't find any examples about how to scan input with this.
I tried to write simple code, but there are some errors.
I have no idea how to edit it.
Could anyone help me to solve this problem?

Any help is appreciated.
Thank you very much!
Max

Brian Candler

unread,
Apr 16, 2020, 7:21:11 AM4/16/20
to golang-nuts
inputParagraph.Text = fmt.Scanf("%s", input) // Error: assignment mismatch: 1 variable but fmt.Scanf returns 2 values

The error message is clear: fmt.Scanf returns two values (BTW, neither of them is the value extracted).  Check the documentation for fmt.Scanf here:

outputParagraph.Text += fmt.Printf("The string you entered is: %s", input) // multiple-value fmt.Printf() in single-value context

In this case you want fmt.Sprintf instead:

洪嘉鴻

unread,
Apr 17, 2020, 4:15:16 AM4/17/20
to golang-nuts
I think that you misunderstand my meaning.
The input I wondered is from the keyboards, like this.
I have no idea how to implement it with termui.
Besides, I edit the code to print something after closing the UI.
However, the result seems strange.
The result appears with the UI and then disappeared with the UI.

Thanks for your replying.
Max

Brian Candler於 2020年4月16日星期四 UTC+8下午7時21分11秒寫道:

Brian Candler

unread,
Apr 17, 2020, 6:52:01 AM4/17/20
to golang-nuts
Well, it's always good to fix the errors :-)

In short, you'll need to get a string from termui, and then use fmt.Sscanf instead of fmt.Scanf.

A quick google for "go termui text input" turns up these as the first two hits:

They answer your question.  The second is a contributed text input widget which hasn't been merged into the mainline source, but you could use termui from this fork.

洪嘉鴻

unread,
Apr 20, 2020, 1:49:52 AM4/20/20
to golang-nuts
I've seen the hits you offered.
However, I couldn't see any examples with the hits.
Besides, the version of termui I used is v3, which is the latest version.
There isn't any functions about the input.
The second is that I want to print something as normal after exit the UI.

Thanks for your replying and information.
Max
 

Brian Candler於 2020年4月17日星期五 UTC+8下午6時52分01秒寫道:

洪嘉鴻

unread,
May 5, 2020, 3:06:08 AM5/5/20
to golang-nuts
I've tried the links you offered and test the source code.
However, it occurred fatal error.
I have no idea what causes the problem.
The error is as the following picture.

Error.PNG


Thank you very much!

Max



Brian Candler於 2020年4月17日星期五 UTC+8下午6時52分01秒寫道:
Reply all
Reply to author
Forward
0 new messages