捕获间隔的用户输入

发布于 2024-12-04 22:49:23 字数 751 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2024-12-11 22:49:23

改为

r := bufio.NewReader() *Reader

阅读

r := bufio.NewReader(os.Stdin)

即可解决问题。

原始的咒语是不正确的,因为您似乎只是从规范复制并粘贴了方法的签名,但是spec 定义签名,而不是调用的示例,因此其中的 *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".

千纸鹤带着心事 2024-12-11 22:49:23

如果您查看 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文