Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the FQDN of the local host?

Tags:

go

fqdn

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.

like image 758
Kevin Bailey Avatar asked Oct 17 '25 16:10

Kevin Bailey


1 Answers

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

like image 82
Medhi Bechina Avatar answered Oct 19 '25 12:10

Medhi Bechina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!