从控制台读取多个单词的字符串

huangapple go评论80阅读模式
英文:

Read multi word string from console

问题

我意识到以下代码只读取一个单词字符串:

fmt.Scan(&sentence)

如何读取多个单词的字符串 - 也就是说,字符串sentence应该存储包含多个单词的字符串。

英文:

I realized that the following only reads a single word string -

fmt.Scan(&sentence)

How do I read multi word string - as in, the string sentence should store a string that contains multiple words.

答案1

得分: 5

可以使用InputReader从控制台扫描和打印多个单词。

解决方案代码如下:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	inputReader := bufio.NewReader(os.Stdin)
	input, _ := inputReader.ReadString('\n')
	fmt.Println(input)
}

控制台输入:

Let's Go!!!

控制台输出:

Let's Go!!!

注意:

要运行一个GOLANG程序,打开命令提示符或PowerShell,导航到程序文件所在的目录,并输入以下命令:

go run file_name.go
英文:

One can use the InputReader also to scan and print multiple words from the console.

The solution code is as follows:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	inputReader := bufio.NewReader(os.Stdin)
	input, _ := inputReader.ReadString('\n')
	fmt.Println(input)
}

Console Input:

Let's Go!!!

Console Output:

Let's Go!!!

Note:

To run a GOLANG program, open the command prompt or powershell, navigate to the directory where the program file is present and type in the following command:

go run file_name.go

答案2

得分: 0

你的问题涉及扫描以空格分隔的输入。fmt.Scan的定义(https://golang.org/pkg/fmt/#Scan)如下所示:

Scan从标准输入中扫描文本,将连续的以空格分隔的值存储到连续的参数中。换行符被视为空格。它返回成功扫描的项目数。如果成功扫描的项目数少于参数数目,err将报告原因。

因此,根据定义,输入会一直扫描到找到第一个空格为止。要扫描,比如说直到在命令行上遇到\n,你可以使用来自评论(https://stackoverflow.com/questions/26071813/scanning-spaces-from-stdin-in-go)中的代码:

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
    return scanner.Text()
}
if err := scanner.Err(); err != nil {
    fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

此外,这个线程可能会有用:https://groups.google.com/forum/#!topic/golang-nuts/r6Jl4D9Juw0

英文:

Your question refers to scanning space separated input. The definition of fmt.Scan https://golang.org/pkg/fmt/#Scan states:

Scan scans text read from standard input, storing successive space-
separated values into successive arguments. Newlines count as space.
It returns the number of items successfully scanned. If that is less
than the number of arguments, err will report why.

So, by definition, input is scanned up until the first space is found. To scan, let's say until you hit a \n on the command line you can use the code from the comment https://stackoverflow.com/questions/26071813/scanning-spaces-from-stdin-in-go:

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
    return scanner.Text()
}
if err := scanner.Err(); err != nil {
    fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

Also this thread might be useful: https://groups.google.com/forum/#!topic/golang-nuts/r6Jl4D9Juw0

huangapple
  • 本文由 发表于 2015年2月17日 17:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/28558711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定