doumu2172 2017-09-14 15:55 采纳率: 0%
浏览 47
已采纳

Golang:fmt.Printf stdout的挂起发生在fmt.scan的for循环旁边

As title, it's hard for me to describe the problem. So I showed the sample code, input and output below.

package main

import (
    "fmt"
)

func main() {
    for i := 1; i <= 3; i++ {
        var a, b, c string
        fmt.Scan(&a)
        fmt.Scan(&b)
        fmt.Scan(&c)
        fmt.Printf("%s
", a)
    }
}

Input

a b c
d e f
g h i

Expected Output

a
d
g

However, the actual output printed "a", "d" first. Then, after pressing return (enter) key, "g" was printed. This was actual output.


Update

If I pasted whole nine characters, the third iteration was hung. However, if I merely enter character one after another, it met the expected output.

  • 写回答

1条回答 默认 最新

  • dtwupu6414 2017-09-14 17:30
    关注

    How did you enter that input without a return (enter) ?

    If you run this interactively and actually typing in all the input I think you'll understand the reason for the output.

    You will have to:

    • Type a b c - nothing will output yet as the third Scan does not know if c is all you'll type
    • Press return (enter) - only at this point you'll see the a output
    • Do the same for the other lines

    Take a look at the doc for Scan which explains this as well:

    https://golang.org/pkg/fmt/#Scan

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法