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.
解决方案
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)
}
}