bufio.NewReader总是创建一个新行:/

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

bufio.NewReader always creates a new line :/

问题

我使用这个来收集输入并显示输入,但是当我这样做时,"something"文本将显示在新的一行,但我希望文本在同一行显示,有什么想法吗?

func main() {
    fmt.Println("Example")
    print("example: ")
    in := bufio.NewReader(os.Stdin)
    input, err := in.ReadString('\n')
    if err != nil {
        fmt.Println("Error: ", err)
    }

    fmt.Print(input, "something")
}
英文:

I use this, to collect a input and display the input then, but when I do it like this the "something" text will be displayed in a new line, but I want the text to be displayed in the same line, any ideas?

func main() {
	fmt.Println("Example")
	print("example: ")
	in := bufio.NewReader(os.Stdin); 
	input, err := in.ReadString('\n');
	if err != nil {
    	fmt.Println("Error: ", err)
	}

	fmt.Println(input, "something")
}

答案1

得分: 3

input中删除换行符。例如,

package main

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

func main() {
	fmt.Println("Example")
	print("example: ")
	in := bufio.NewReader(os.Stdin)
	input, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	fmt.Println(strings.TrimRight(input, "\n"), "something")
}

输入/输出:

Example
example: some input
some input something
英文:

Trim the newline from input. For example,

package main

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

func main() {
	fmt.Println("Example")
	print("example: ")
	in := bufio.NewReader(os.Stdin)
	input, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("Error: ", err)
		return
	}
	fmt.Println(strings.TrimRight(input, "\n"), "something")
}

Input/Output:

Example
example: some input
some input something

答案2

得分: 2

fmt.Println()自动在输出的末尾添加换行符。

你可以尝试使用Printf,它接受一个“格式字符串”和一系列的输入。

一个例子如下:

func main() {
  fmt.Println("Example")
  print("example: ")
  in := bufio.NewReader(os.Stdin); 
  input, err := in.ReadString('\n');
  if err != nil {
      fmt.Println("Error: ", err)
  }

  fmt.Printf("something : %s", input)
}

在这个例子中,%s是一个字符串类型的占位符。
所有的占位符可以在fmt godoc中找到:http://golang.org/pkg/fmt/

另外,如果输入本身在末尾有一个换行符,你可以使用strings包中的Trim函数来去掉\n字符。

英文:

fmt.Println() automatically appends a newline to the end of it's output.

You could try using Printf, which takes a "format string" and a list of inputs.

An example would be:

func main() {
  fmt.Println("Example")
  print("example: ")
  in := bufio.NewReader(os.Stdin); 
  input, err := in.ReadString('\n');
  if err != nil {
      fmt.Println("Error: ", err)
  }

  fmt.Printf("something : %s", input)
}

In this case, %s is a placeholder for a string type.
All the placeholders can be found in the fmt godoc: http://golang.org/pkg/fmt/

Also if the input itself has a newline at the end, you can use Trim from the package strings to pull off the \n character.

答案3

得分: 1

在Windows上(不确定是否相关),之前的答案都对我没用。所以这里是我在https://groups.google.com/d/msg/golang-nuts/hWoESdeZ398/qDbTogJhj88J找到的解决方法,该链接指向https://play.golang.org/p/_9N_RwmBvd

基本思路是,不要使用strings.TrimRight(input, "\n"),而是包含一个\r字符,像这样strings.TrimRight(input, "\r\n")
这样可以解决OP的问题。

修复输入剥离的OP代码!

func main() {
    fmt.Println("Example")
    print("example: ")
    in := bufio.NewReader(os.Stdin); 
    input, err := in.ReadString('\n');
    if err != nil {
        fmt.Println("Error: ", err)
    }

    fmt.Println(strings.TrimRight(input, "\r\n"), "something")
}

PS:对不起,我只是希望能帮助别人节省20分钟的谷歌搜索时间!

英文:

On windows (not sure if that is relevant) none of the previous answers worked for me. So here's the resolution I found on: https://groups.google.com/d/msg/golang-nuts/hWoESdeZ398/qDbTogJhj88J which links to https://play.golang.org/p/_9N_RwmBvd

The basic idea is that rather than using strings.TrimRight(input, "\n") you would include a \r character, like strings.TrimRight(input, "\r\n")
This fixes the OPs issue.

OP code with fixed input stripping!

func main() {
    fmt.Println("Example")
    print("example: ")
    in := bufio.NewReader(os.Stdin); 
    input, err := in.ReadString('\n');
    if err != nil {
        fmt.Println("Error: ", err)
    }

    fmt.Println(strings.TrimRight(input, "\r\n"), "something")
}

PS: Sorry for the necro I'm just hoping to save someone from wasting 20 minutes googling!

huangapple
  • 本文由 发表于 2011年11月17日 04:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/8158023.html
匿名

发表评论

匿名网友

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

确定