25

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

fmt.Scanf("%s",&input)

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
av192
  • 444
  • 1
  • 4
  • 10

3 Answers3

40

you can use bufio.Reader and os.Stdin:

import(
  "bufio"
  "os"
)

in := bufio.NewReader(os.Stdin)

line, err := in.ReadString('\n')
Andy
  • 49,085
  • 60
  • 166
  • 233
rchlin
  • 509
  • 4
  • 5
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Jun 12 '15 at 14:53
  • I think the solution is to get input by the use of "bufio" library in golang. http://golang.org/pkg/bufio/ – rchlin Jun 12 '15 at 15:00
  • The read string will be `This is a string\n` whereas I wanted it to be `This is a string`. – VIAGC Apr 22 '22 at 20:24
  • Golang v1.2.0 works for me with another way: ```go in := bufio.NewReader(os.Stdin) line, err = in.ReadString('\n') ``` Without ":=", only equals symbol – Facundo Padilla Jun 19 '23 at 04:15
38

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\n", 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 (
  "bufio"
  "os"
)

func main(){
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan() // use `for scanner.Scan()` to keep reading
    line := scanner.Text()
    fmt.Println("captured:",line)
}
sethammons
  • 743
  • 6
  • 10
-5

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    The program runs by itself and does not wait for the user input when I use this function. – av192 Dec 12 '14 at 04:53
  • @av192 strange considering their respective implementations (https://github.com/golang/go/blob/439b32936367c3efd0dadab48dd51202e1a510f1/src/fmt/scan.go#L131-L148) – VonC Dec 12 '14 at 06:58
  • 2
    The referenced doc mentions Scan. From Scan: "... storing successive space-separated values into successive arguments." If you want to capture the whole input from stdin, I suggest bufio.NewScanner(os.Stdin). – sethammons Feb 19 '17 at 16:48
  • 13
    this does not the solve the problem of taking a string containing spaces as input. ie mystr = "Hello there" – DevX May 31 '18 at 08:11
  • @av192, note that if you pass the variable itself, instead of the address of the variable ( fmt.Scanln(var) instead of fmt.Scanln(&var) ), the issue of the program not waiting for user input crops up. Can someone throw light on the reason behind this behaviour? – Zac May 18 '20 at 09:48