捕获带有空格的用户输入

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

capture spaced user input

问题

我正在尝试使用Go来捕获用户输入,但运气不太好。我可以让非空格的单词工作:

var s string
println("输入字符串:")
fmt.Scan(&s)

然而,Go的文档说scan会以空格和换行符为分隔符。所以我认为我需要设置bufio.Reader的ReadLine。这是我的尝试,但它无法编译:

package main

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

const delim = '\n'
const file = "file"

func main() {

    r := bufio.NewReader() *Reader

    println("输入字符串:")
    line, err := r.ReadString(delim)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(line)

}

错误:

1.go:14: 调用bufio.NewReader时参数不足
1.go:14: 未定义:Reader

那么,我该如何定义“Reader”?如果它被定义了,这样捕获输入作为一个字符串的方式是否正确,以“\n”为分隔符,而不是以空格为分隔符?或者我应该完全做些不同的事情?

提前感谢。

英文:

I am trying to capture user input in Go with little luck. I can get non-spaced words to work:

var s string
println("enter string:")
fmt.Scan(&s)

However, the Go documentation says that scan will delimit at spaces and new lines. So I think I have to set up bufio.Reader's ReadLine. Here is my attempt, which will not compile:

package main

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

const delim = '\n'
const file = "file"

func main() {

	r := bufio.NewReader() *Reader

	println("enter string:")
	line, err := r.ReadString(delim)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(line)

}

errors:

1.go:14: not enough arguments in call to bufio.NewReader
1.go:14: undefined: Reader

So, how do I define "Reader"? And if it was defined, would this be the correct way to capture the input as a string, delimited at "\n", and not at the space? Or should I be doing something completely different?

Thanks in advance.

答案1

得分: 3

r := bufio.NewReader() *Reader

更改为

r := bufio.NewReader(os.Stdin)

以修复问题。

原始的写法是不正确的,因为你似乎只是从规范中复制并粘贴了方法的签名,但规范定义的是签名,而不是调用的示例,所以其中的*Reader是方法的返回类型(变量r将具有的类型)。而方法的唯一参数被定义为rd io.Reader;这个接口正好由os.Stdin符号方便地实现,似乎非常适合你的任务。

附注:
考虑阅读《学习 Go》文档部分中的所有文档,特别是《Effective Go》。

英文:

Change

r := bufio.NewReader() *Reader

to read

r := bufio.NewReader(os.Stdin)

to fix the problem.

The original encantation is incorrect because you seem to just copied and pasted the method's signature from the spec, but the spec defines the signature, not an example of a call, so *Reader in there is the method's return type (the type your variable r will have). And the method's sole argument is defined to be rd io.Reader; that interface is conveniently implemented by the os.Stdin symbol which seems like a perfect match for your task.

P.S.
Consider reading all the docs in the "Learning Go" documentation section, especially "Effective Go".

答案2

得分: 1

如果你查看bufio.NewReader的文档,它接受一个io.Reader类型的参数(这是有道理的,因为它接受一个普通的读取器并将其缓冲,类似于Java中的java.io.BufferedReader,它也接受一个Reader参数)。什么是io.Reader?查看它的文档,它是一个接口,指定任何具有Read方法的东西。许多类型都有Read方法;最常见的是*os.File。因此,你可以使用os.Open等方法构造一个File

f, _ := os.Open(file)
r := bufio.NewReader(f)
英文:

If you look at the documentation for bufio.NewReader, it takes an argument of type io.Reader (which makes sense, because it takes a normal reader and makes it buffered, similar to java.io.BufferedReader in Java, which also takes a Reader argument). What is io.Reader? Looking at its documentation, it is an interface, specifying anything that has a Read method. Many types have a Read method; most commonly, *os.File. So you can construct a File using os.Open etc.

f, _ := os.Open(file)
r := bufio.NewReader(f)

huangapple
  • 本文由 发表于 2011年9月17日 13:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/7452641.html
匿名

发表评论

匿名网友

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

确定