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.
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.
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)
}