I am new to Go.
I wrote the program below to allow a user to enter the name.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter name:")
name, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
fmt.Println(name == "nixon")
}
I ran the program and entered my name nixon
Why is it that the output of fmt.Println(name == "nixon")
is always false
?
name
terminal input includes trailing newlines. For example,Output (Linux):
Output (Windows):
Trim trailing newlines. For example,
}
Output (Linux):
Output (Windows):