func (*Reader) ReadString
func (b *Reader) ReadString(delim byte) (string, error)
ReadString reads until the first occurrence of delim in the input,
returning a string containing the data up to and including the
delimiter.
name
terminal input includes trailing newlines. For example,
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter name:")
name, err := reader.ReadString('
')
if err != nil {
panic(err)
}
fmt.Printf("%q
", name)
fmt.Println(name == "nixon")
}
Output (Linux):
Enter name:nixon
"nixon
"
false
Output (Windows):
Enter name:nixon
"nixon
"
false
Trim trailing newlines. For example,
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter name:")
name, err := reader.ReadString('
')
if err != nil {
panic(err)
}
fmt.Printf("%q
", name)
name = strings.TrimRight(name, "
")
fmt.Printf("%q
", name)
fmt.Println(name == "nixon")
}
Output (Linux):
Enter name:nixon
"nixon
"
"nixon"
true
Output (Windows):
Enter name:nixon
"nixon
"
"nixon"
true