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")
}
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" {
}