How to read from user input without the \n ?
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
port, _ = reader.ReadString('\n')
port = strings.Replace(port, "\n", "", -1)
here : port, _ = reader.ReadString('\n')
thanks
评论:
exch:
bussiere:You can remove the newlines with strings.TrimSpace, or try parsing with fmt.Fscanf and friends.
f00b4z:thks
bussiere:Maybe try with
bufio.NewScanner(os.Stdin)
f00b4z:i want to read an input and newScanner doesn't seems to work
epiris:I meant
scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { fmt.Printf("%s", scanner.Text()) }
1lann:I think what you may be asking is to get input characters before a newline is entered? Stdin will be line buffered if it is attached to a terminal, you will want to use the vt100 interface for finer control of user input on Linux. You can do this multiple ways, interact with /dev/tty directly, exec stty, or just search for a library to do these things for you (what I suggest) for a more portable general approach.
A short, fast approach would be to use a slice operation.
port = port[:len(port)-1]
