James has a blog

where he writes things down
where he writes things down
  • rss
  • archive
  • Setting the HTTP User Agent in Go

    Here’s the smallest amount of code I could come up with to set the user agent when making a HTTP request in Go:

    type UserAgent string
    
    func (u UserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
        r.Header.Set("User-Agent", string(u))
        return http.DefaultTransport.RoundTrip(r)
    }
    
    http.DefaultClient.Transport = UserAgent("my-great-program")
    
    • September 10, 2019 (7:04 am)
© 2012–2026 James has a blog