duandaodao6951 2016-06-04 06:36
浏览 19
已采纳

等同于scanf%[^ ]的语言

for go language


fmt.Scanln("%s",&v)


this should store entire line as string inside v variable right..? but when I am printing I get only the first word and everything after the space is not captured.

  • 写回答

2条回答 默认 最新

  • dsgd4654674 2016-06-04 07:07
    关注

    The Scan family of functions scan space-separated tokens.

    If you read the fmt documentation it will be obvious why you'll get only the first word of the input.

    Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space. It returns the number of items successfully scanned. If that is less than the number of arguments, err will report why.

    bufio.Scan is what you need.

    // read standard input as a set of lines.
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
            fmt.Println(scanner.Text()) // Println will add back the final '
    '
    }
    
    if err := scanner.Err(); err != nil {
            fmt.Fprintln(os.Stderr, "reading standard input:", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案