无法正确地取消引用指针并从内存地址数组中获取实际值

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?

Ok few problems :

  • Why are you returning *&nserv? Go is NOT C, please stop everything you're doing and read Effective Go.

  • Your nslookup function returns a slice of *net.NS, that's a slice of pointers, so fmt.Println is printing the right thing, if you want more details you could use fmt.Printf with %#vor %#q modifier to see how the data actually looks.

Example:

package main

import "fmt"
import "net"
import "os"

var host string

func nslookupString(nserv []*net.NS) (hosts []string) {
    hosts = make([]string, len(nserv))
    for i, host := range nserv {
        hosts[i] = host.Host
    }
    return
}

func nslookupNS(host string) []*net.NS {
    nserv, err := net.LookupNS(host)
    if err != nil {
        fmt.Println("Error occured during NS lookup", err)
    }
    return nserv
}

func init() { //initilizing global arguments is usually done in init()
    if len(os.Args) == 2 {
        host = os.Args[1]
    }
}

func main() {
    if host == "" {
        fmt.Println("You need to enter a host!")
        os.Exit(1)
    }
    fmt.Println("Nameserver information:", host)
    ns := nslookupNS(host)
    fmt.Printf("   NS records String: %#q
", nslookupString(ns))
    fmt.Printf("   NS records net.NS: %q
", ns)
    for _, h := range ns {
        fmt.Printf("%#v
", h)
    }

}