Overview: I was capturing strings with fmt.Scanln, but one of my needs is that the string must have spaces so I started to use reader.ReadString, I have to check the presence of a suffix in that string to make some replacing, so I used strings.HasSuffix. With strings captured with fmt.Scanln there was no problema but now that I use reader.ReadString it is unabled to find the given suffix in the string.
Is there a clue on what's going on?
Here is a test that isolate the problem, use it in desktop installed Go, not online.
package main
import (
"bufio"
"fmt"
"strings"
"os"
"reflect"
)
const TOKEN string = ":="
func validate(expr string) {
fmt.Println("var type: ", reflect.TypeOf(expr))
if strings.Contains(expr, TOKEN) {
fmt.Println(expr, "contains works")
} else {
fmt.Println("error with contains")
}
if strings.HasSuffix(expr, TOKEN) {
fmt.Println(expr, "suffix works")
} else {
fmt.Println("error with suffix")
}
}
func main() {
var expr2 string
reader := bufio.NewReader(os.Stdin)
fmt.Print("type something finished with ':='")
expr1, _ := reader.ReadString('
')
fmt.Scanln(&expr2)
fmt.Println("validation with reader: ")
validate(expr1)
fmt.Println()
fmt.Println("validation with Scanln: ")
validate(expr2)
}