How can I do DNS lookup in Go?

Go’s "net" package in the stdlib has standard DNS client functions. For example, given a domain name, you can look up IP addresses:

package main

import (

func main() {
	ips, err := net.LookupIP("")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not get IPs: %v\n", err)
	for _, ip := range ips {
		fmt.Printf(" IN A %s\n", ip.String())

Go’s stdlib either uses the C stdlib (via cgo), or a pure Go DNS resolver. On my machine, it uses cgo, which you can see with:

$ GODEBUG=netdns=9 go run main.go
go package net: using cgo DNS resolver
go package net: hostLookupOrder( = cgo IN A IN A 2a00:1450:4009:80d::200e
Tagged #golang, #dns, #networking, #programming.

Similar posts

More by Jim

👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business? Read more about me or Get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.