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

huangapple go评论123阅读模式
英文:

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

问题

想要获取“30 of month”,但得到了“30”。

  1. package main
  2. import "fmt"
  3. func main() {
  4. var s string
  5. fmt.Scanln(&s)
  6. fmt.Println(s)
  7. return
  8. }
  9. $ go run test.go
  10. 31 of month
  11. 31
  12. > Scanln 类似于 Scan但在换行符处停止扫描并且在最后一项之后必须有一个换行符或 EOF
英文:

Want "30 of month" but get "30"

  1. package main
  2. import "fmt"
  3. func main() {
  4. var s string
  5. fmt.Scanln(&s)
  6. fmt.Println(s)
  7. return
  8. }
  9. $ go run test.go
  10. 31 of month
  11. 31

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

答案1

得分: 11

The fmt Scan family scan space-separated tokens.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var s1 string
  7. var s2 string
  8. fmt.Scanln(&s1,&s2)
  9. fmt.Println(s1)
  10. fmt.Println(s2)
  11. return
  12. }

你可以尝试使用 bufio scan

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. scanner := bufio.NewScanner(os.Stdin)
  9. for scanner.Scan() {
  10. s := scanner.Text()
  11. fmt.Println(s)
  12. }
  13. if err := scanner.Err(); err != nil {
  14. os.Exit(1)
  15. }
  16. }
英文:

The fmt Scan family scan space-separated tokens.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var s1 string
  7. var s2 string
  8. fmt.Scanln(&s1,&s2)
  9. fmt.Println(s1)
  10. fmt.Println(s2)
  11. return
  12. }

You can try bufio scan

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. scanner := bufio.NewScanner(os.Stdin)
  9. for scanner.Scan() {
  10. s := scanner.Text()
  11. fmt.Println(s)
  12. }
  13. if err := scanner.Err(); err != nil {
  14. os.Exit(1)
  15. }
  16. }

答案2

得分: 9

如果你真的想包含空格,你可以考虑使用fmt.Scanf()和格式%q,例如:

  1. package main
  2. import "fmt"
  3. func main() {
  4. var s string
  5. fmt.Scanf("%q", &s)
  6. fmt.Println(s)
  7. return
  8. }

运行它:

  1. $ go run test.go
  2. "31 of month"
  3. 31 of month
英文:

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:

  1. package main
  2. import "fmt"
  3. func main() {
  4. var s string
  5. fmt.Scanf("%q", &s)
  6. fmt.Println(s)
  7. return
  8. }

Run it and:

  1. $ go run test.go
  2. "31 of month"
  3. 31 of month

答案3

得分: 4

这是一个工作中的程序:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. var strInput string
  9. fmt.Println("输入一个字符串:")
  10. scanner := bufio.NewScanner(os.Stdin)
  11. if scanner.Scan() {
  12. strInput = scanner.Text()
  13. }
  14. fmt.Println(strInput)
  15. }

该程序读取像 " d skd a efju N" 这样的字符串,并将相同的字符串作为输出打印出来。

英文:

Here is the working program

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. var strInput string
  9. fmt.Println("Enter a string ")
  10. scanner := bufio.NewScanner(os.Stdin)
  11. if scanner.Scan() {
  12. strInput = scanner.Text()
  13. }
  14. fmt.Println(strInput)
  15. }

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

huangapple
  • 本文由 发表于 2016年1月7日 11:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/34647039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定