I have the following code:
reader := bufio.NewReader(os.Stdin)
fmt.Print("room: width x length: ")
inStr, _ := reader.ReadString('
')
result := strings.Split(inStr, "x")
string1, _ := strconv.ParseFloat(result[0], 64)
string2, _ := strconv.ParseFloat(result[1], 64)
fmt.Print(string2)
At the last print statement, if i print string1
it returns the right value, but if i try to print string2
it returns 0, no matter what value i input to the console.
Does anyone know why this is happening? Thanks!
Replace
result := strings.Split(inStr, "x")
with
result := strings.Split(strings.TrimSpace(inStr), "x")
As string contains so your second array element contains it too.
Also I really suggest to look at error messages before posting such kind of questions. You could see strconv.ParseFloat: parsing "23 ": invalid syntax
as result of next code
string2, err := strconv.ParseFloat(result[1], 64)
if err != nil {
fmt.Println(e)
}