I need to be able selecting from which IP address my HTTP request should be sent. In my code right now, for DNS caching i'm using the following according to this:
defaultTransport.DialContext = func(ctx context.Context, network string, addr string) (conn net.Conn, err error) {
separator := strings.LastIndex(addr, ":")
ips, err := r.LookupHost(ctx, addr[:separator])
if err != nil {
return nil, err
}
for _, ip := range ips {
conn, err = net.Dial(network, ip+addr[separator:])
if err == nil {
break
}
}
return
}
In order to specify the local address, the only way i found is by using the following:
defaultTransport.DialContext = (&net.Dialer{
LocalAddr: localAddr,
}).DialContext
How can i achieve combining the above?