amitsaha / echorand.me

Source for https://echorand.me
6 stars 11 forks source link

Go test stdin doesn't work #16

Open lcd1232 opened 6 months ago

lcd1232 commented 6 months ago

Hi. I read your article https://echorand.me/posts/go-test-stdin/ and it seems like your example doesn't work it returns fatal error: all goroutines are asleep - deadlock!

amitsaha commented 2 months ago

Hi @lcd1232 thanks for reporting, i will have to check. can you share the output of your go version please?

lcd1232 commented 2 months ago

go version go1.20.14 darwin/arm64

amitsaha commented 2 months ago

Hi @lcd1232 i am assuming you are referring to the second example?

    r, _ := io.Pipe()
    scanner := bufio.NewScanner(r)
    msg := "Your name please? Press the Enter key when done"
    fmt.Fprintln(os.Stdout, msg)

    scanner.Scan()
    if err := scanner.Err(); err != nil {
        t.Fatal(err)
    }
    name := scanner.Text()
    if len(name) == 0 {
        t.Log("empty input")
    }
    t.Logf("You entered: %s\n", name)
}

It does wait for me:

Your name please? Press the Enter key when done

My go version: go version go1.21.0 darwin/arm64

However, if i execute the program in the Go playground, I get the error you are referring to: https://go.dev/play/p/h3_CPMDplJv

Are you executing the program on your own system?