That makes it much easier to write tests for it, also for the package author itself.
It neatly solves most of these problems by providing an orthogonal API.
An *additional* TCP dialer for convenience is no mistake.
I hadn't looked at this until now and I tend to agree with your concerns.
The DialOption thing seems a bit too clever for its own good.
Placing the options behind an opaque interface barrier doesn't
look great to me.
I'm pretty sure it's too late now though - this will go into 1.1.
There is an external workaround. You can define a package that really
does define such a struct, and a method on it that converts
to dial options.
type DialOpts struct {
Deadline time.Time
Network string
etc
}
func (d *DialOpts) Opts() []net.DialOption {
var opts []net.DialOption
if !d.Deadline.IsZero() {
opts = append(opts, net.Deadline(d.Deadline))
}
if d.Network != "" {
opts = append(opts, net.Network(d.Network))
}
... etc
return opts
}
As long as you pass around the struct and not []net.DialOption,
you can do all you need to (with some allocation cost per dial)
One advantage of net.DialOpt is that there's no
need to assume that the zero value of a field implies the default.
I'm not sure that's a hugely strong strong argument though - net
uses a struct under the hood anyway as it happens.
On 01.04 16:24, Brad Fitzpatrick wrote:I would like to advocate having dialing with options not marked as
> [+golang-dev]
>
> Opinions wanted. See below.
API stable and have it experimental for Go 1.1. Is this possible
from the API perspective?
I do advocate a struct if this is not possible.
Additionally with the struct one could do e.g:
package tor
type Dialer struct {
net.Dialer
NoConnectionReuse bool
}