且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从控制台线读取输入?

更新时间:2021-09-09 06:45:13

我不确定块有什么问题

reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println(text)

它在我的机器上工作。但是,对于下一个块,您需要一个指向要分配输入的变量的指针。尝试用 fmt.Scanln(& text2)替换 fmt.Scanln(text2)。不要使用 Sscanln ,因为它解析已经在内存中的字符串而不是标准输入。如果你想做一些你想做的事情,把它替换为 fmt.Scanf(%s,& ln)

As it works on my machine. However, for the next block you need a pointer to the variables you're assigning the input to. Try replacing fmt.Scanln(text2) with fmt.Scanln(&text2). Don't use Sscanln, because it parses a string already in memory instead of from stdin. If you want to do something like what you were trying to do, replace it with fmt.Scanf("%s", &ln)

如果这仍然不起作用,您的罪魁祸首可能是一些奇怪的系统设置或一个有问题的IDE。

If this still doesn't work, your culprit might be some weird system settings or a buggy IDE.