2014-12-11 02:52 阅读 397


I'm just starting with GO and I understand that SCANF uses spaces as a separator in GO.


I cant really find a way to accepts inputs that contain spaces as valid characters.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dtqu3278 dtqu3278 2014-12-11 06:30

    When the user presses enter it signifies the end of input

    That looks like fmt.Scanln

    Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.

    点赞 评论 复制链接分享
  • dongmaopan5738 dongmaopan5738 2015-06-12 14:49

    you can use bufio.Reader and os.Stdin:

    in := bufio.NewReader(os.Stdin)
    line, err := in.ReadString('
    点赞 评论 复制链接分享
  • doupeng6890 doupeng6890 2017-02-19 17:01

    Similar to @chlin's answer, use bufio to capture whole lines.

    The fmt Scan methods store each space separated value into a successive arguments. Three arguments on stdin would require something like:

    package main
    import "fmt"
    func main() {
        var day, year int
        var month string
        fmt.Scanf("%d %s %d", &day, &month, &year)
        fmt.Printf("captured: %d %s %d
    ", day, month, year)

    If you don't know the full format of what you will be reading and just want the line, use bufio:

    package main
    import (
    func main(){
        scanner := bufio.NewScanner(os.Stdin)
        scanner.Scan() // use `for scanner.Scan()` to keep reading
        line := scanner.Text()
    点赞 评论 复制链接分享