0

I want to read some input from stdin and then display it. At the moment I am doing this like this:

in := bufio.NewReader(os.Stdin);
input, err = in.ReadString('\n');
if err != nil {
    fmt.Println("Error: ", err)
    os.Exit(1)
}
fmt.Printf("This is your", input)

...but after running this and entering some input it is always displaying my input twice like this:

This is just a test
This is your This is just a test

Is there anyway to remove the first line?

georgeok
  • 5,321
  • 2
  • 39
  • 61
  • Is this your real code? You haven't told `fmt.Printf` to print anything except a static string with no newline at the end. – Matt K Nov 28 '11 at 21:30

2 Answers2

4

I haven't yet tried anything with the package, but I guess it could be helpful in this case: exp/terminal. Specifically the ReadPasword function documentations is:

ReadPassword reads a line of input from a terminal without local echo.
This is commonly used for inputting passwords and other sensitive data.
The slice returned does not include the \n.
zzzz
  • 87,403
  • 16
  • 175
  • 139
1

I assume your first line is just your echoed input text? That's actually a function of the process' terminal. Since the go runtime treats Stdin like any other file, you don't have direct access to the terminal attributes. However you might be able to hack something together with CGO and the approach described here.

Community
  • 1
  • 1
laslowh
  • 8,482
  • 5
  • 34
  • 45