gpt4 book ai didi

console - 类似于 getchar 的功能

转载 作者:IT老高 更新时间:2023-10-28 13:00:37 28 4
gpt4 key购买 nike

是否有类似于 C 的 getchar 的 Go 函数能够处理控制台中的制表符?我想在我的控制台应用程序中完成某种完成。

最佳答案

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('CHARACTER TABULATION')。

但是根据您的需要(检测选项卡)C 的 getchar() 不适合,因为它需要用户按回车键。你需要的是像 @miku 提到的 ncurses 的 getch()/readline/jLine 之类的东西。有了这些,您实际上就在等待一个按键。

所以你有多种选择:

  1. 使用 ncurses/readline 绑定(bind),例如 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

关于console - 类似于 getchar 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14094190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com