类似于getchar的函数

41

是否有类似于C语言中的getchar函数的Go语言函数,能够处理控制台中按下Tab键的情况?我想在我的控制台应用程序中实现某种形式的自动完成。


1
找到一个很好的自定义示例:https://github.com/SimonWaldherr/golang-examples/blob/master/advanced/getchar.go - mtso
6个回答

25

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 这样的东西。使用这些工具,您实际上等待单个按键。

所以您有多个选择:

  1. 使用 ncurses / readline 绑定,例如 https://code.google.com/p/goncurses/ 或类似的库,如 https://github.com/nsf/termbox
  2. 自己编写,可以参考 http://play.golang.org/p/plwBIIYiqG
  3. 使用 os.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


17

假设你想要无缓冲输入(不需要按回车键),这段代码能在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)+")")
    }
}

8
执行完命令后,终端会停留在非回显状态。添加defer exec.Command("stty", "-F", "/dev/tty", "echo")可以解决这个问题。 - Grumdrig
2
延迟执行exec.Command("stty", "-F", "/dev/tty", "echo").Run()对我无效,我必须键入“reset”:( - gdonald
5
不要进行低效、容易出错、不可移植等大规模的 stty 执行操作,因为你可以直接使用 golang.org/x/crypto/ssh/terminal 中的 terminal.MakeRaw 函数。 - Dave C
如果延迟(defer)“不起作用”,实际上构建和编译项目以运行它,而不是使用go run *.go。这对我有用。 - Aaron

9
其他答案中提到了以下方法:
  • 使用cgo

  • os.Execstty

    • 不可移植
    • 效率低下
    • 容易出错
  • 使用使用/dev/tty的代码

    • 不可移植
  • 使用GNU readline包

    • 如果它是C readline的包装器或者是用上述技术之一实现的,则效率低下
    • 否则可以

然而,对于简单情况,这很容易,只需使用Go项目的子存储库中的一个包。

[编辑:此答案以前使用的golang.org/x/crypto/ssh/terminal包已被弃用;它已经移动到golang.org/x/term。代码/链接已相应地更新。]

基本上,使用term.MakeRawterm.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 Renich
@RikRenich 抱歉,在测试后我将最终的 log 更改为 fmt,但没有调整导入。现在已修复。 - Dave C

8
感谢Paul Rademacher - 这个方法可行(至少在Mac上):
package 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
}

1
在Linux上也完美运行。 - hookenz
2
使用/dev/tty是不可移植的。绝对不能忽略错误!不要为每个“字符”翻转终端进出原始模式,也不要为每个“字符”重新打开“/dev/tty”。这实际上并没有获取字符,而是获取了多达三个字节。 - Dave C

1

1- 你可以使用 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)
}

仍然需要在 openSUSE Leap 42.1 上为我按下回车键。 :( - lfree
抱歉我之前忘了提到conio.h只能在Windows系统中使用!我已经尝试过这个这个,虽然能够运行,但是getch()函数总是返回-1。 - lfree
最后,我采用了@blinry的方法。 - lfree
1
第二种方法适用于openSUSE Leap42.1。非常感谢! :) - lfree
Linux版本实际上只能使用终端作为标准输入,而不能使用管道。 - hoijui
显示剩余2条评论

-5

你也可以使用ReadRune:

reader := bufio.NewReader(os.Stdin)
// ...
char, _, err := reader.ReadRune()
if err != nil {
    fmt.Println("Error reading key...", err)
}

符文类似于字符,因为GoLang实际上没有字符,为了尝试支持多种语言/Unicode等。


1
ReadRune 一次只读取一个字符,但它仅在控制台按下回车时触发;这就是为什么它不能解决 OP 的问题。 (我想知道为什么你没有更多的赞,所以我试了一下 :)) - Christian Rondeau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接