Custom DNS resolver for the default HTTP client in Go

The default HTTP client in Go uses the default DNS resolver available on the machine. There are some cases where you might want to use a different DNS resolver.

I want to quickly show how you can change the DNS resolver for the default HTTP client. We are using this same method for our custom DNS resolver in my company: Violetnorth

// Changing DNS resolverpackage main

import (

func main() {
var (
dnsResolverIP = "" // Google DNS resolver.
dnsResolverProto = "udp" // Protocol to use for the DNS resolver
dnsResolverTimeoutMs = 5000 // Timeout (ms) for the DNS resolver (optional)

dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Duration(dnsResolverTimeoutMs) * time.Millisecond,
return d.DialContext(ctx, dnsResolverProto, dnsResolverIP)

dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)

http.DefaultTransport.(*http.Transport).DialContext = dialContext
httpClient := &http.Client{}

// Testing the new HTTP client with the custom DNS resolver.
resp, err := httpClient.Get("")
if err != nil {
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {


It only takes a few lines but you have to overwrite the Dial function in the Resolver which is inside the Dialer which is inside the Transport function.

It might seem weird but it actually makes a lot of sense, follow the functions from top to bottom, DefaultTransport -> DialContext -> Dialer -> Resolver -> Dial

We are changing the Dial function which dials the DNS server to resolve the request, so it should be inside the Resolver. But this whole thing is in the DialContext of the actual HTTP request.

You have to dial the DNS resolver first, resolve the IP, then dial that resolved IP to make the HTTP request.

Anyways that’s pretty much it. Changing the DNS resolver in the default HTTP client.

University of Toronto, Computer Engineering, architected and implemented reliable infrastructures and worked as the lead developer for multiple startups.