duanchongchu5177 2014-06-08 12:40 采纳率: 0%
浏览 25
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • douba4933 2014-06-08 13:02
    关注

    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)
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。