是否有类似于C语言中的getchar
函数的Go语言函数,能够处理控制台中按下Tab键的情况?我想在我的控制台应用程序中实现某种形式的自动完成。
C的getchar()
示例:
#include <stdio.h>
void main()
{
char ch;
ch = getchar();
printf("Input Char Is :%c",ch);
}
Go的等效语句:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
fmt.Printf("Input Char Is : %v", string([]byte(input)[0]))
// fmt.Printf("You entered: %v", []byte(input))
}
最后一个被注释的行仅显示,当您按下 tab
时,第一个元素是 U+0009(“字符制表符”)。
但是对于您的需要(检测制表符),C 的 getchar()
不适用,因为它要求用户按回车键。您需要的是像 @miku 提到的 ncurses' getch() / readline / jLine 这样的东西。使用这些工具,您实际上等待单个按键。
所以您有多个选择:
ncurses
/ readline
绑定,例如 https://code.google.com/p/goncurses/ 或类似的库,如 https://github.com/nsf/termboxos.Exec
运行 stty 或 jLine。参考文献:
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
假设你想要无缓冲输入(不需要按回车键),这段代码能在UNIX系统上实现:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
// restore the echoing state when exiting
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
var b []byte = make([]byte, 1)
for {
os.Stdin.Read(b)
fmt.Println("I got the byte", b, "("+string(b)+")")
}
}
defer exec.Command("stty", "-F", "/dev/tty", "echo")
可以解决这个问题。 - Grumdriggolang.org/x/crypto/ssh/terminal
中的 terminal.MakeRaw
函数。 - Dave C使用cgo
os.Exec
,stty
使用使用/dev/tty
的代码
使用GNU readline包
然而,对于简单情况,这很容易,只需使用Go项目的子存储库中的一个包。
[编辑:此答案以前使用的golang.org/x/crypto/ssh/terminal
包已被弃用;它已经移动到golang.org/x/term
。代码/链接已相应地更新。]
基本上,使用term.MakeRaw
和term.Restore
将标准输入设置为原始模式(检查错误,例如如果stdin不是终端);然后你可以直接从os.Stdin
读取字节,或者更可能通过bufio.Reader
读取(以提高效率)。
例如,类似于以下内容:
package main
import (
"bufio"
"fmt"
"log"
"os"
"golang.org/x/term"
)
func main() {
// fd 0 is stdin
state, err := term.MakeRaw(0)
if err != nil {
log.Fatalln("setting stdin to raw:", err)
}
defer func() {
if err := term.Restore(0, state); err != nil {
log.Println("warning, failed to restore terminal:", err)
}
}()
in := bufio.NewReader(os.Stdin)
for {
r, _, err := in.ReadRune()
if err != nil {
log.Println("stdin:", err)
break
}
fmt.Printf("read rune %q\r\n", r)
if r == 'q' {
break
}
}
}
import
中添加“fmt”即可。 - Rik Renichlog
更改为 fmt
,但没有调整导入。现在已修复。 - Dave Cpackage main
import (
"bytes"
"fmt"
"github.com/pkg/term"
)
func getch() []byte {
t, _ := term.Open("/dev/tty")
term.RawMode(t)
bytes := make([]byte, 3)
numRead, err := t.Read(bytes)
t.Restore()
t.Close()
if err != nil {
return nil
}
return bytes[0:numRead]
}
func main() {
for {
c := getch()
switch {
case bytes.Equal(c, []byte{3}):
return
case bytes.Equal(c, []byte{27, 91, 68}): // left
fmt.Println("LEFT pressed")
default:
fmt.Println("Unknown pressed", c)
}
}
return
}
/dev/tty
是不可移植的。绝对不能忽略错误!不要为每个“字符”翻转终端进出原始模式,也不要为每个“字符”重新打开“/dev/tty”。这实际上并没有获取字符,而是获取了多达三个字节。 - Dave C1- 你可以使用 C.getch()
:
这在Windows命令行中有效,读取一个字符而不需要按Enter键:
(在shell(终端)中运行输出二进制文件,而不是在管道或编辑器中运行。)
package main
//#include<conio.h>
import "C"
import "fmt"
func main() {
c := C.getch()
fmt.Println(c)
}
2- 对于 Linux(在 Ubuntu 上测试):
package main
/*
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char ch = 0;
struct termios old = {0};
fflush(stdout);
if( tcgetattr(0, &old) < 0 ) perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if( tcsetattr(0, TCSANOW, &old) < 0 ) perror("tcsetattr ICANON");
if( read(0, &ch,1) < 0 ) perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
return ch;
}
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.getch())
fmt.Println()
}
请参考:
在Linux中等同于getch()和getche()的函数是什么?
为什么我在Linux上找不到<conio.h>头文件?
3- 这个也可以工作,但需要“Enter”:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
r := bufio.NewReader(os.Stdin)
c, err := r.ReadByte()
if err != nil {
panic(err)
}
fmt.Println(c)
}
你也可以使用ReadRune:
reader := bufio.NewReader(os.Stdin)
// ...
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println("Error reading key...", err)
}
符文类似于字符,因为GoLang实际上没有字符,为了尝试支持多种语言/Unicode等。