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

huangapple go评论84阅读模式
英文:

Can't dereference pointers properly and get actual values out of an array of memory addresses

问题

我最近几天开始学习Go语言,主要依赖于语言规范和包文档,但是我在解读net.LookupNS的正确用法时遇到了问题。
由于它是一个指针类型,返回一个包含NS服务器值内存地址的数组,我想要访问实际的值/解引用数组。

以下是代码示例:

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())
}

给定例如google.com,它显示如下:

Nameserver information: google.com
   NS records: [0xc2100376f0 0xc210037700 0xc210037710 0xc210037720]

我希望看到的不是内存地址,而是解引用后的值,例如:

   NS records: ["ns1.google.com", "ns2.google.com", "ns3.google.com", "ns4.google.com"]

显然,我更希望它们作为字符串的数组/切片,但问题是我只能通过以下方式获取一个实际的nameserver:

func nslookup() *net.NS {
  // 函数的其余部分
return &nserv[0] // 这将返回第一个nameserver
}

上述代码返回如下结果:

Nameserver information: google.com
   NS records: &{ns1.google.com.} 

虽然这至少返回了实际的值而不是内存地址,但它需要使用索引,这不太灵活,并且格式不太用户友好。
另外,直接将[]*net.NS结构转换为字符串是不可能的。

问题是:如何获取一个nameserver的数组,而不是内存地址,最好作为一个字符串的数组/切片?

英文:

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

得分: 5

好的,以下是翻译好的内容:

好几个问题:

  • 为什么你返回 *&nserv?Go 不是 C,请停止你正在做的一切,阅读 Effective Go

  • 你的 nslookup 函数返回一个 *net.NS 的切片,也就是指针的切片,所以 fmt.Println 打印的是正确的内容,如果你想要更多细节,你可以使用 fmt.Printf 并使用 %#v%#q 修饰符来查看数据的实际样子。

示例:

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("NS查找期间发生错误", err)
    }
    return nserv
}

func init() { //通常在init()中初始化全局参数
    if len(os.Args) == 2 {
        host = os.Args[1]
    }
}

func main() {
    if host == "" {
        fmt.Println("你需要输入一个主机名!")
        os.Exit(1)
    }
    fmt.Println("Nameserver 信息:", host)
    ns := nslookupNS(host)
    fmt.Printf("   NS 记录字符串: %#q\n", nslookupString(ns))
    fmt.Printf("   NS 记录 net.NS: %q\n", ns)
    for _, h := range ns {
        fmt.Printf("%#v\n", h)
    }

}
英文:

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\n", nslookupString(ns))
	fmt.Printf("   NS records net.NS: %q\n", ns)
	for _, h := range ns {
		fmt.Printf("%#v\n", h)
	}

}

huangapple
  • 本文由 发表于 2014年6月8日 20:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/24106253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定