15

When I run go vet the following error is output:

client.go:2345: assignment copies lock value to tr: net/http.Transport contains sync.Mutex
exit status 1

client.go:2345:

var tr http.Transport

// Setup TLS
if clientConfig.TLSEnabled {
    tr = http.Transport{ // This is line 2345
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
            MinVersion: tls.VersionTLS11,
        },
    }
}

How can I get around this warning? It's not stopping my builds; but, it's a warning and I don't want warnings.

1 Answers1

25

You should be creating a *http.Transport pointer, instead of a value

tr = &http.Transport{
JimB
  • 104,193
  • 13
  • 262
  • 255
  • How do we know that? Isn't `http.Transport{}` without a pointer is valid statement? – Nilesh Dec 23 '21 at 05:01
  • 2
    @Nilesh, there are lots of valid statements which are not logical. Unfortunately you can't readily see the mutex field from the docs, so this may not be obvious at first glance, but because the Transport type has all pointer receivers, should always be reused, and is safe for concurrent access, it's very unlikely that someone would choose a non-pointer value. In that rare event, the `vet` warning points out the mutex copy. – JimB Dec 23 '21 at 14:12