Menu Close

Go – How to get canonical name of host in Go

Here, we will help you to understand how to get canonical name of host in Go. We will learn about LookupCNAME() method to get canonical name in go.

Function prototype:

func LookupCNAME(host string) (cname string, err error)

Input parameters:

host: Host name like techieindoor.com

Return value:

LookupCNAME() function in net package returns canonical name for the given host. LookupCNAME() method does not return an error if host does not contain DNS “CNAME” records, as long as host resolves to address records.

Example with code:

package main

import (
  "fmt"
  "net"
)


func main() {

    c_name, err := net.LookupCNAME("techieindoor.com")

    if err == nil {

        fmt.Println(c_name)

    } else {

        fmt.Print("Lookup CNAME failed with err: ", err)

    }
}

Output:

$ go run sample.go

techieindoor.com.

Example with invalid host name:

package main

import (
  "fmt"
  "net"
)


func main() {

    c_name, err := net.LookupCNAME("helloInvalid.com")

    if err == nil {

        fmt.Println(c_name)

    } else {

        fmt.Print("Lookup CNAME failed with err: ", err)

    }
}

Output:

$: go run sample.go

Lookup CNAME failed with err: lookup helloInvalid.com: no such host

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org

Posted in golang, net

Leave a Reply

Your email address will not be published. Required fields are marked *