捕获间隔的用户输入
我试图在 Go 中捕获用户输入,但运气不佳。我可以让非空格单词工作:
var s string
println("enter string:")
fmt.Scan(&s)
但是,Go 文档说扫描将在空格和新行处进行分隔。所以我想我必须设置bufio.Reader的ReadLine。这是我的尝试,它不会编译:
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)
}
错误:
1.go:14: not enough arguments in call to bufio.NewReader
1.go:14: undefined: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
改为
阅读
即可解决问题。
原始的咒语是不正确的,因为您似乎只是从规范复制并粘贴了方法的签名,但是spec 定义签名,而不是调用的示例,因此其中的
*Reader
是方法的返回类型(变量r
将具有的类型)。该方法的唯一参数被定义为 rd io.Reader;该接口可以通过 os.Stdin 符号方便地实现,这似乎非常适合您的任务。聚苯乙烯
考虑阅读“学习 Go” 文档部分中的所有文档,尤其是“Effective Go”。
Change
to read
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 variabler
will have). And the method's sole argument is defined to berd io.Reader
; that interface is conveniently implemented by theos.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".
如果您查看 bufio.NewReader 的文档,它需要一个 io.Reader 类型的参数(这是有道理的,因为它需要一个普通的阅读器并使其缓冲,类似于 Java 中的
java.io.BufferedReader
,它也采用Reader
参数)。什么是 io.Reader?查看其文档,它是一个接口,指定具有 Read 方法的任何内容。许多类型都有一个Read
方法;最常见的是*os.File
。因此,您可以使用os.Open
等构造一个File
。If you look at the documentation for
bufio.NewReader
, it takes an argument of typeio.Reader
(which makes sense, because it takes a normal reader and makes it buffered, similar tojava.io.BufferedReader
in Java, which also takes aReader
argument). What isio.Reader
? Looking at its documentation, it is an interface, specifying anything that has aRead
method. Many types have aRead
method; most commonly,*os.File
. So you can construct aFile
usingos.Open
etc.