类似getchar的功能

问题描述 投票:0回答:7

是否有类似于 C 的 Go 函数

getchar
能够处理控制台中的 Tab 键按下?我想在我的控制台应用程序中完成某种完成。

console go getchar
7个回答
25
投票

C 的

getchar()
示例:

#include <stdio.h>
void main()
{
    char ch;
    ch = getchar();
    printf("Input Char Is :%c",ch);
}

同等:

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()
不适合,因为它需要用户按 Enter 键。你需要的是像 @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
投票

假设您需要无缓冲输入(无需按 Enter),这可以在 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)+")")
    }
}

9
投票

这里的其他答案建议如下:

  • 使用cgo

  • os.Exec
    stty

    • 不可携带
    • 效率低下
    • 容易出错
  • 使用使用

    /dev/tty

    的代码
    • 不可携带
  • 使用 GNU readline 包

    • 如果它是 C 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
        }
    }
}

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
投票

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 上找不到


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)
}

0
投票
package main

import (
    "bufio"
    "golang.org/x/crypto/ssh/terminal"
    "log"
    "os"
)

func main() {
    state, err := terminal.MakeRaw(0)
    if err != nil {
        log.Fatalln("Could not make stdin a raw terminal:", err.Error())
    }
    defer terminal.Restore(0, state)

    reader := bufio.NewReader(os.Stdin)
    for byte, err := reader.ReadByte(); err == nil; byte, err = reader.ReadByte(){
        log.Println("Read byte:", byte)
    }
    
}

-5
投票

您还可以使用ReadRune:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.