How to input ip address from keyboard in Go?

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

How to input ip address from keyboard in Go?

问题

我正在尝试在Go语言中从键盘输入IP地址。当我尝试使用bufio输入IP地址时,无法将"*bufio.Convert"类型转换为"string"类型。当我尝试使用Scanf()输入IP地址时,程序会跳过第二个变量的输入。如果我想将输入转换为字符串,我该怎么做?

  1. import (
  2. "bufio"
  3. "fmt"
  4. "net"
  5. "os"
  6. )
  7. func checkerror(err error) {
  8. if err != nil {
  9. fmt.Println("Error:", err)
  10. }
  11. }
  12. func main() {
  13. typeofoperation := bufio.NewScanner(os.Stdin)
  14. typeofoperation.Scan()
  15. typeofoperation.Text()
  16. addr := bufio.NewScanner(os.Stdin)
  17. addr.Scan()
  18. addr.Text()
  19. if typeofoperation.Text() == "tcp" {
  20. address, err := net.ResolveTCPAddr("tcp", addr.Text())
  21. checkerror(err)
  22. conn, err1 := net.DialTCP("tcp", nil, address)
  23. checkerror(err1)
  24. fmt.Println(conn, "TCP end")
  25. } else if typeofoperation.Text() == "ip" {
  26. address, err := net.ResolveIPAddr("ip", addr.Text())
  27. checkerror(err)
  28. conn, err1 := net.DialIP("ip", nil, address)
  29. checkerror(err1)
  30. fmt.Println(conn, "IP end")
  31. }
  32. fmt.Println("End")
  33. }

以上是你提供的代码的翻译。

英文:

Im trying to input IP address from keyboard in Go. When Im trying to input IP address using bufio i can`t convert "*bufio.Convert" type to "string" type. When Im trying to input ip address using Scanf() program skips input of second variable. What I must to do if i want to convert input to string?

  1. import (
  2. "bufio"
  3. "fmt"
  4. "net"
  5. "os"
  6. )
  7. func checkerror(err error) {
  8. if err != nil {
  9. fmt.Println("Error:=", err)
  10. }
  11. }
  12. func main() {
  13. typeofoperation := bufio.NewScanner(os.Stdin)
  14. typeofoperation.Scan()
  15. typeofoperation.Text()
  16. //fmt.Println("IP or TCP dial?")
  17. //fmt.Println("Input Address")
  18. //fmt.Scanf("%s", &typeofoperation)
  19. //fmt.Scanf("%s", &addr)
  20. addr := bufio.NewScanner(os.Stdin)
  21. addr.Scan()
  22. addr.Text()
  23. if typeofoperation == "tcp" {
  24. address, err := net.ResolveTCPAddr("tcp", addr)
  25. checkerror(err)
  26. conn, err1 := net.DialTCP("tcp", nil, address)
  27. checkerror(err1)
  28. fmt.Println(conn, "TCP end")
  29. } else if typeofoperation == "ip" {
  30. address, err := net.ResolveIPAddr("ip", addr)
  31. checkerror(err)
  32. conn, err1 := net.DialIP("ip", nil, address)
  33. checkerror(err1)
  34. fmt.Println(conn, "IP end")
  35. }
  36. fmt.Println("End")
  37. }

答案1

得分: 1

问题在于你在这里使用的是Scanner的实例进行比较,而不是输入值。你应该将Text()返回的值存储在一个变量中,并将其用于比较。

  1. typeofoperation_input := typeofoperation.Text()
  2. add_input := addr.Text()
  3. if typeofoperation_input == "tcp" {
  4. }
英文:

The issue is that you're using Scanner's instance for comparison here not the input. You should store the value returned by Text() in a variable and use it for comparison.

  1. typeofoperation_input := typeofoperation.Text()
  2. add_input := addr.Text()
  3. if typeofoperation_input == "tcp" {
  4. }

huangapple
  • 本文由 发表于 2016年11月22日 20:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/40741719.html
匿名

发表评论

匿名网友

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

确定