This is going to be a simple question to answer, I am sure of it. I am brand new to Go and I can't figure out what is happening.
package main
import (
"fmt"
"bufio"
"os"
"strings"
)
func main() {
fmt.Print("*A basic GoLang/Native example*
")
fmt.Print("Enter your name:
")
reader := bufio.NewReader(os.Stdin)
name, _ := reader.ReadString('
')
name = strings.TrimSuffix(name, "
")
fmt.Printf("Hello, %s, how are you?", name)
reader.ReadString('
')
}
I am removing the newline which is stored in variable name from reading in the user input, but the last Printf statement is truncating Hello, from the line.
I think you are looking for strings.TrimSpace
.
To remove the leading and trailing white spaces use this.
Change this line from
name = strings.TrimSuffix(name, " ")
to
name = strings.TrimSpace(name)
Output:
*A basic GoLang/Native example* Enter your name: Source Cast Hello, Source Cast, how are you?
It is working fine for both Windows and Linux.