Golang ProgramsGolang Programs

Go program to find SRV service record of a domain

The LookupSRV function tries to resolve an SRV query of the given service, protocol, and domain name. The second parameter is "tcp" or "udp". The returned records are sorted by priority and randomized by weight within a priority.
package main
 
import (
	"fmt"
	"net"
)
 
func main() {
	cname, srvs, err := net.LookupSRV("xmpp-server", "tcp", "golang.org")
	if err != nil {
		panic(err)
	}
 
	fmt.Printf("\ncname: %s \n\n", cname)
 
	for _, srv := range srvs {
		fmt.Printf("%v:%v:%d:%d\n", srv.Target, srv.Port, srv.Priority, srv.Weight)
	}
}

The output below demonstrates the CNAME return, followed by the SRV record target, port, priority, and weight separated by a colon.

C:\golang\dns>go run example6.go
cname: _xmpp-server._tcp.golang.org.