In Go, you can perform DNS lookups and resolve domain names to IP addresses using the net
package. Here's an example code snippet:
package main
import (
"fmt"
"net"
)
func main() {
domain := "example.com"
// Resolve the domain name to IP addresses
ipAddresses, err := net.LookupIP(domain)
if err != nil {
fmt.Println("DNS lookup failed:", err)
return
}
// Print all the IP addresses associated with the domain
for _, ipAddress := range ipAddresses {
fmt.Println(ipAddress)
}
}
In the example above, we use the LookupIP
function from the net
package to perform a DNS lookup for the specified domain. It returns a slice of net.IP
addresses associated with the domain. We can then iterate over these addresses and print them.
You can run this code by saving it to a file (e.g., dns_lookup.go
) and executing go run dns_lookup.go
. Replace "example.com"
with the domain name you want to resolve.