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