How to input ip address from keyboard in Go?

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

How to input ip address from keyboard in Go?

问题

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

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

func checkerror(err error) {
	if err != nil {
		fmt.Println("Error:", err)
	}
}

func main() {
	typeofoperation := bufio.NewScanner(os.Stdin)
	typeofoperation.Scan()
	typeofoperation.Text()

	addr := bufio.NewScanner(os.Stdin)
	addr.Scan()
	addr.Text()

	if typeofoperation.Text() == "tcp" {
		address, err := net.ResolveTCPAddr("tcp", addr.Text())
		checkerror(err)
		conn, err1 := net.DialTCP("tcp", nil, address)
		checkerror(err1)
		fmt.Println(conn, "TCP end")
	} else if typeofoperation.Text() == "ip" {
		address, err := net.ResolveIPAddr("ip", addr.Text())
		checkerror(err)
		conn, err1 := net.DialIP("ip", nil, address)
		checkerror(err1)
		fmt.Println(conn, "IP end")
	}

	fmt.Println("End")
}

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

英文:

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?

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

func checkerror(err error) {
   if err != nil {
	    fmt.Println("Error:=", err)
   }
}

func main() {
   typeofoperation := bufio.NewScanner(os.Stdin)
   typeofoperation.Scan()
   typeofoperation.Text()
   //fmt.Println("IP or TCP dial?")
   //fmt.Println("Input Address")
   //fmt.Scanf("%s", &typeofoperation)
   //fmt.Scanf("%s", &addr)
   addr := bufio.NewScanner(os.Stdin)
   addr.Scan()
   addr.Text()
      if typeofoperation == "tcp" {
   	     address, err := net.ResolveTCPAddr("tcp", addr)
	     checkerror(err)
	     conn, err1 := net.DialTCP("tcp", nil, address)
	     checkerror(err1)
	     fmt.Println(conn, "TCP end")
      } else if typeofoperation == "ip" {
	     address, err := net.ResolveIPAddr("ip", addr)
	     checkerror(err)
	     conn, err1 := net.DialIP("ip", nil, address)
	     checkerror(err1)
	     fmt.Println(conn, "IP end")
   }
      fmt.Println("End")
}

答案1

得分: 1

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

typeofoperation_input := typeofoperation.Text()
add_input := addr.Text()

if typeofoperation_input == "tcp" {
}
英文:

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.

typeofoperation_input := typeofoperation.Text()
add_input := addr.Text()

if typeofoperation_input == "tcp" {
}

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:

确定