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.
CodePudding user response:
I assume you're using some IDE.
sometimes they mess up the console output, try to run directly from native terminal instead. Or use vscode.
CodePudding user response:
i had retype your code and i got the same as Sombriks, you must have pressed enter key by mistake.
here is a screenshot
package main
import (
"fmt"
"strconv"
)
func main() {
for {
name := ""
fmt.Print("Enter: ")
fmt.Scanf("%s", &name)
b, _ := strconv.ParseBool(name)
fmt.Printf("%T - %v\n", b, b)
}
}
CodePudding user response:
this issue is only in windows.
Solved by adding the new line in the "%s\n" format strings. As in this answer
the new cross-platform working code :
package main
import (
"fmt"
"strconv"
)
func main() {
for {
var name string
fmt.Print("Enter : ")
fmt.Scanf("%s\n", &name)
b1, _ := strconv.ParseBool(name)
fmt.Printf("%T, %v \n", b1, b1)
}
}