doufa5001 2017-05-06 12:41
浏览 71

Golang:调试接受输入的代码时的奇怪行为

I am considering this simple code:

package main

import (
    "fmt"
    "bufio"
    "io"
    "os"

)
var in *bufio.Reader
var out io.Writer

func init() {
    in = bufio.NewReader(os.Stdin)
    out = os.Stdout
}

func main() {
    str, err := in.ReadString('
')
    fmt.Println("str: ", str, "err: ", err)
}

I am using IntelliJ with the Go plugin. My version of Go is 1.8. If I simply run the code there is no problem: str will be the same as the string I put, and err will be < nil >. If I debug the code I will not have the chance to put anything as input and I will have the following output:

    API server listening at: 127.0.0.1:44824
    str:   err:  EOF

I have no idea of what is happening: what is the first line for? How is it generated? And why the terminal does not "block" to let me write the input? And why err is "EOF" (it is like the part in which I have to put text is simply skipped: why?) ?

Last additional detail: I am executing this simple piece of code because I am actually trying to understand what is happening in a bigger project, in which a similar issue occurs. In fact, in that case I still have to put something by hand, and, again, if I debug the code it returns an error. In this case, however, the err variable contains a different thing: "error".

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度