I make this code for learning:-
package main
import (
"fmt"
"strconv"
)
func main() {
for {
name := ""
fmt.Print("Enter : ")
fmt.Scanf("%s", &name)
b1, _ := strconv.ParseBool(name)
fmt.Printf("%T, %v\n", b1, b1)
}
}
output:
Enter : True
bool, true
Enter : bool, false
Enter :
why there is an extra "Enter: bool, false" line?
edit:
this will happen in Powershell, cmd and bash in windows but not in wsl.
Edit: this was the same issue but it is a different question. and the solution there as I mentioned is outdated, It was updated after I mentioned it in my answer but I have to confirm and check that.
name
to find out what it contains.name
contains in between the call tofmt.Scanf()
andstrconv.ParseBool()
. Those are two operations and the outcome of them surprises you, so split them and see which one is the cause of it.