Get Domain Name in Go: Quick Tutorial

In Golang, you can use the net package to obtain domain names. Specifically, you can use the net.LookupHost() function, which takes a parameter – the domain name to be queried – and returns a slice of strings containing the list of IP addresses corresponding to that domain.

Here is an example code:

package main

import (
	"fmt"
	"net"
)

func main() {
	domain := "example.com"
	ips, err := net.LookupHost(domain)
	if err != nil {
		fmt.Println("Failed to lookup domain:", err)
		return
	}
	fmt.Println("IP addresses for", domain, ":")
	for _, ip := range ips {
		fmt.Println(ip)
	}
}

Running the above code will output a list of IP addresses corresponding to the domain name. Note that this example code will use the default DNS server for domain resolution. If you need to use a specific DNS server, you can use the net.Resolver type for more advanced domain resolution operations.

bannerAds