Dial with a specific address / interface ? Golang

后端 未结 1 1319
别跟我提以往
别跟我提以往 2020-12-29 14:54

I have two network interfaces on my computer ( eth0 and eth1) and I\'m trying to Dial a connection using a specific one (eth1). Given the statement that Go is a system langu

相关标签:
1条回答
  • 2020-12-29 15:03

    When you pull the address from an interface, it's of type *net.IPnet wrapped in a net.Addr interface, which contains and address and netmask not an address and port. You can use the IP address however to create a new TCPAddr after asserting it as a *net.IPnet

        ief, err := net.InterfaceByName("eth1")
        if err !=nil{
                log.Fatal(err)
        }
        addrs, err := ief.Addrs()
        if err !=nil{
                log.Fatal(err)
        }
    
        tcpAddr := &net.TCPAddr{
            IP: addrs[0].(*net.IPNet).IP,
        }
    
    0 讨论(0)
提交回复
热议问题