I started picking Go in the past couple of days, relying mostly on the language specification and package documentation, however I have problem deciphering the correct usage of net.LookupNS. Since it's a pointer type, returning an array of memory addresses of NS server values, I want to access the actual values / dereference the array.
The Program:
package main
import "fmt"
import "net"
import "os"
var host string
func args() {
if len(os.Args) != 2 {
fmt.Println("You need to enter a host!")
} else {
host = os.Args[1]
}
if host == "" {
os.Exit(0)
}
}
func nslookup() []*net.NS {
nserv, err := net.LookupNS(host)
if err != nil {
fmt.Println("Error occured during NS lookup", err)
}
return *&nserv
}
func main() {
args()
fmt.Println("Nameserver information:", host)
fmt.Println(" NS records:", nslookup())
}
Given e.g. google.com, it displays the following:
Nameserver information: google.com
NS records: [0xc2100376f0 0xc210037700 0xc210037710 0xc210037720]
Instead of the memory address locations, I would like to see the dereferenced values, e.g:
NS records: ["ns1.google.com", "ns2.google.com", "ns3.google.com", "ns4.google.com"]
Now obviously, I would prefer them as an array/slice of strings, but the problem is that the only way I can get an actual nameserver out is as follows:
func nslookup() *net.NS {
// The rest of the function
return *&nserv[0] // This returns the first nameserver
The above returns the following:
Nameserver information: google.com
NS records: &{ns1.google.com.}
While this at least returns the actual value instead of a memory address, it requires indexing, which isn't very flexible and it's not formatted in a very user-friendly format. Also, direct conversion of the []*net.NS struct to string is not possible.
The Problem: How do I get an array of nameservers, instead of memory addresses out, preferably as an array/slice of strings?