Skip to content

proposal: net/http: make nil *Client equivalent to zero Client #26036

Closed
@ghost

Description

net.Resolver in currently allows the following (ignoring #24330):

var r *net.Resolver
fmt.Println(r.LookupHost(context.Background(), "example.com"))

Note that nil *Resolver is equivalent to the zero Resolver.

I propose that using the nil net/http.*Client also be permitted. This is useful when allowing callers to specify a *Client to use for HTTP operations:

type Config struct {
	Client *http.Client
}

func (c *Config) Get() {
	resp, err := c.Client.Get("https://example.com")
	// Rather than:
	client := c.Client
	if client == nil {
		client = http.DefaultClient
	}
	resp, err := client.Get("https://example.com")
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions