在net.DialTCP中指定本地IP地址

package main

import (
    "fmt"
    "net"
)

func main() {

    var localaddr net.TCPAddr
    var remoteaddr net.TCPAddr
    localaddr.IP = net.ParseIP("192.168.1.104")
    localaddr.Port = 6000
    remoteaddr.IP = net.ParseIP("192.168.1.104")
    remoteaddr.Port = 5000

    if localaddr.IP == nil || remoteaddr.IP == nil {
        fmt.Println("error")
    }

    if _, err := net.DialTCP("tcp", &localaddr, &remoteaddr); err != nil {
        fmt.Println(err)
    }

    fmt.Println("End")

}

If the function specify local IP address, it always reports a run time error "dial tcp 192.168.1.104:5000: An invalid argument was supplied." I'm confused, should it always be a nil for local IP address ?

go version : 1.1 Beta OS: Win7 64bit

Tried in go 1.0.3, it seemed OK

Go's net package, like most such implementations, recognizes port-only syntax for connecting to localhost:

:5000

is equivalent to

<my-ip-address>:5000

Thus, you can do:

net.Dial("tcp", ":5000")

No, it is ok to have laddr argument specified in DialTCP. The problem is that the specified port (6000) is outside of default Win7 dynamic port range and couldn't be used for outgoing connection. It works on Linux and Mac, actually (if you don't forget to change the IP address to one of your own system).