按 Enter 时指定的默认值

发布于 2025-01-15 16:42:43 字数 532 浏览 2 评论 0原文

我希望在按 Enter 时分配一个默认值而不给出一个。

根据我的经验,Go Playground 不处理 fmt.Scan 输入,因此我在此处引用代码:
(如果可能,请告诉我如何操作!)

package main

import (
    "fmt"
    "time"
)

func main() {
    t := 0
    fmt.Print("seconds? ")
    fmt.Scan(&t)
    time.Sleep(time.Duration(t) * time.Second)
    fmt.Println("in", t)
}

我已将 t 的值初始化为 0,但是当我按 Enter 时没有给出value 程序会等待,直到我给出一些值。我对错误检查不感兴趣(如果可能的话)。我只希望代码接受一次 Enter 按键,如 0 Enter

I want a default value to be assigned when pressing Enter without giving one.

From my experience Go Playground doesn't handle fmt.Scan inputs, so I quote the code here:
(If it's possible, tell me how please!)

package main

import (
    "fmt"
    "time"
)

func main() {
    t := 0
    fmt.Print("seconds? ")
    fmt.Scan(&t)
    time.Sleep(time.Duration(t) * time.Second)
    fmt.Println("in", t)
}

I've initialized the value of t to 0 but when I press Enter without giving a value the program waits until I give some value. I'm not interested in error checking (if that's possible). I just want the code to accept a single Enter press, as 0 Enter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

埖埖迣鎅 2025-01-22 16:42:43

只需使用 fmt.Scanln() 而不是 fmt.Scan()

fmt.Scanln(&t)

引用自fmt.Scanln()

Scanln 与 Scan 类似,但在换行符处停止扫描,并且在最后一项之后必须有换行符或 EOF。

另一方面,fmt.Scan() 不会在换行符处停止,而是:

Scan 扫描从标准输入读取的文本,将连续的空格分隔值存储到连续的参数中。 换行符算作空格。

一般来说,我建议使用 bufio.Scanner 按行读取输入,并对这些行执行您想要的任何操作(检查其是否为空,或按照您想要的方式解析它)。 bufio.NewScanner() 允许您使用(预定义的) string 作为源(与 strings.NewReader()——为了方便测试)或者如果你想读取用户输入,则传递os.Stdin

Simply use fmt.Scanln() instead of fmt.Scan():

fmt.Scanln(&t)

Quoting from fmt.Scanln():

Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.

On the other hand, fmt.Scan() does not stop at a newline, but:

Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space.

In general I suggest to use bufio.Scanner to read input by lines, and do whatever you want to with the lines (check if its empty, or parse it the way you want). bufio.NewScanner() allows you to use a (predefined) string as the source (combined with strings.NewReader()–for easy testing) or pass os.Stdin if you want to read user input.

守不住的情 2025-01-22 16:42:43

您可以使用bufio.Reader

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
    "time"
)

func main() {
    var t int

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("seconds? ")
    for {
        tStr, err := reader.ReadString('\n')
        tStr = strings.TrimSpace(tStr)

        t, err = strconv.Atoi(tStr)
        if err != nil {
            fmt.Println("invalid input, please write a number")
            continue
        }
        break
    }

    time.Sleep(time.Duration(t) * time.Second)
    fmt.Println("in", t)
}

You can use bufio.Reader

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
    "time"
)

func main() {
    var t int

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("seconds? ")
    for {
        tStr, err := reader.ReadString('\n')
        tStr = strings.TrimSpace(tStr)

        t, err = strconv.Atoi(tStr)
        if err != nil {
            fmt.Println("invalid input, please write a number")
            continue
        }
        break
    }

    time.Sleep(time.Duration(t) * time.Second)
    fmt.Println("in", t)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文