猿问

终端 Go 应用程序中的多行输入

我需要让用户向控制台输入多行文本。


这是我的代码:


package main


import (

    "bufio"

    "fmt"

    "os"

)


func main() {

    for {

        fmt.Println("How to read all lines here?")

        in := bufio.NewReader(os.Stdin)

        result, err := in.ReadString('\n')

        if err != nil {

            fmt.Println(err)

        }

        fmt.Println("\nresult")

        fmt.Println(result)     


    }

}

我粘贴在控制台中:


    Hello

    World

它输出:


How to read all lines here?


        Hello

        World



result



How to read all lines here?


result

        Hello


How to read all lines here?


result

        World


How to read all lines here?


result



How to read all lines here?

但我希望它是:


         How to read all lines here?


                Hello

                World



        result



        How to read all lines here?


        result

                Hello

                World


        How to read all lines here?

我想我需要使用类似的东西EOF而不是'\n' But 怎么做呢?


更新

peterSo 的答案有效,除非我尝试从剪贴板粘贴文本,中间有一个或多个空行,例如:


Hello


World

它打印


Enter Lines:

Hello


WorldResult:

Hello


Enter Lines:

更新 2

伟大的更新 peterSO 的答案现在甚至适用于带有空行的文本。


德玛西亚99
浏览 278回答 3
3回答

jeck猫

缓冲一组行并检测一组行的结尾。例如,package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os")func main() {&nbsp; &nbsp; scn := bufio.NewScanner(os.Stdin)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Enter Lines:")&nbsp; &nbsp; &nbsp; &nbsp; var lines []string&nbsp; &nbsp; &nbsp; &nbsp; for scn.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line := scn.Text()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(line) == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Group Separator (GS ^]): ctrl-]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if line[0] == '\x1D' {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines = append(lines, line)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if len(lines) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Result:")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, line := range lines {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if err := scn.Err(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if len(lines) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}安慰:输入行:你好世界^]结果:你好世界输入行:告别世界^]结果:告别世界输入行:^]要终止一组行,请在空行上输入:< ctrl+]>< Enter>。要终止输入,请输入一行:< ctrl+]>< Enter>。
随时随地看视频慕课网APP

相关分类

Go
我要回答