How to use fmt.scanln read from a string separated by spaces
Asked Answered
D

4

15

Want "30 of month" but get "30"

package main

import "fmt"

func main() {
    var s string
    fmt.Scanln(&s)
    fmt.Println(s)
    return
}

$ go run test.go
31 of month
31

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

Danielladanielle answered 7/1, 2016 at 3:47 Comment(0)
F
11

The fmt Scan family scan space-separated tokens.

package main

import (
    "fmt"
)

func main() {
    var s1 string
    var s2 string
    fmt.Scanln(&s1,&s2)
    fmt.Println(s1)
    fmt.Println(s2)
    return
}

You can try bufio scan

package main
import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        s := scanner.Text()
        fmt.Println(s)
    }
    if err := scanner.Err(); err != nil {
        os.Exit(1)
    }
}
Fulgor answered 7/1, 2016 at 4:3 Comment(7)
thx...but if I put fmt.Println(s) outside of the loop, no values will be output. how can i stop the loopDanielladanielle
func main() { scanner := bufio.NewScanner(os.Stdin) if ok := scanner.Scan();ok { fmt.Println(scanner.Text()) } } scanner.Scan() will return bool when end scanFulgor
Second snippet worked great. Why are you checking for error after for loop ? would the if else block be executed here in case of an error ? Because there is a infinite for loop before it.Moneylender
@Fulgor - I have the same query as Ishan above, doesn't checking for the error after fmt.Println(s), break things? you are checking scanner.Err() after printing the scan?Mobility
bufio solution is great!Hobbyhorse
I have to use an if statement instead of for loop, so that the program will proceed after the first input. If I use a for loop, the program will keep asking me for input forever. How would I get out of the loop if I used a for loop?Alberthaalberti
I agree with @ZassBurgh
S
9

If you really want to include the spaces, you may consider using fmt.Scanf() with format %q a double-quoted string safely escaped with Go syntax , for example:

package main

import "fmt"

func main() {
    var s string
    fmt.Scanf("%q", &s)
    fmt.Println(s)
    return
}

Run it and:

$ go run test.go
"31 of month"
31 of month
Stencil answered 7/1, 2016 at 4:12 Comment(2)
With this approach i'm getting: expected quoted string trying to input "hello world" stringSlack
Not a very good approach, as it forces the user to use double quotes around their input in the consoleBaddie
B
5

Here is the working program

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    var strInput string
    fmt.Println("Enter a string ")
    scanner := bufio.NewScanner(os.Stdin)

    if scanner.Scan() {
        strInput = scanner.Text()
    }
    
    fmt.Println(strInput)
}

which reads strings like " d skd a efju N" and prints the same string as output.

Burgh answered 10/11, 2018 at 15:19 Comment(3)
question specifically asked about fmt.scanln, not how to achieve same result with different methodGizmo
I voted up as this question is accessable from search engine and it indeed worksFlynn
gondo the answer is "you can't do this with scanln"Brochure
A
0

I'm going to add to @trquoccuong answer so that this is visible for everyone.

package main
import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    if scanner.Scan() {
        s := scanner.Text()
        fmt.Println(s)
    }
    if err := scanner.Err(); err != nil {
        os.Exit(1)
    }
}

This way your code doesn't get stuck in the forloop and continues with the next task.

Appling answered 2/8, 2024 at 14:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.