Is there a Go function similar to C's getchar
able to handle tab press in console? I want to make some sort of completion in my console app.
与getchar类似的功能
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
8条回答 默认 最新
- douxu3315 2012-12-31 06:32关注
C's
getchar()
example:#include <stdio.h> void main() { char ch; ch = getchar(); printf("Input Char Is :%c",ch); }
Go equivalent:
package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) input, _ := reader.ReadString(' ') fmt.Printf("Input Char Is : %v", string([]byte(input)[0])) // fmt.Printf("You entered: %v", []byte(input)) }
The last commented line just shows that when you press
tab
the first element is U+0009 ('CHARACTER TABULATION').However for your needs (detecting tab) C's
getchar()
is not suitable as it requires the user to hit enter. What you need is something like ncurses' getch()/ readline/ jLine as mentioned by @miku. With these, you actually wait for a single keystroke.So you have multiple options:
Use
ncurses
/readline
binding, for example https://code.google.com/p/goncurses/ or equivalent like https://github.com/nsf/termboxRoll your own see http://play.golang.org/p/plwBIIYiqG for starting point
use
os.Exec
to run stty or jLine.
refs:
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/zhBE5MH4n-Q
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/S9AO_kHktiY
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/icMfYF8wJCk
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
- ¥15 数据可视化Python
- ¥15 要给毕业设计添加扫码登录的功能!!有偿
- ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
- ¥15 微信公众号自制会员卡没有收款渠道啊
- ¥15 stable diffusion
- ¥100 Jenkins自动化部署—悬赏100元
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条