How do you find the FQDN of the local host?

Kevin Bailey picture Kevin Bailey · Oct 3, 2015 · Viewed 8.4k times · Source

How do you find the FQDN of the local host in Go?

BTW: net.LookupAddr() doesn't work on Windows. So that's not an option.

Answer

Medhi Bechina picture Medhi Bechina · Dec 17, 2015

By default there is no short way.

os.Hostname() doesn't provide the Fully Qualified Domain Name by default.

cmd := exec.Command("/bin/hostname", "-f")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    log.Errorf(err)
}
fqdn := out.String()
fqdn = fqdn[:len(fqdn)-1] // removing EOL