why fmt.Scanf("%s", &name) print the previous value

Issue

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.
enter image description here

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.

Solution

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.

enter image description here

Answered By – Sombriks

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.