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

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

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

问题

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

以下是代码示例:

  1. package main
  2. import "fmt"
  3. import "net"
  4. import "os"
  5. var host string
  6. func args() {
  7. if len(os.Args) != 2 {
  8. fmt.Println("You need to enter a host!")
  9. } else {
  10. host = os.Args[1]
  11. }
  12. if host == "" {
  13. os.Exit(0)
  14. }
  15. }
  16. func nslookup() []*net.NS {
  17. nserv, err := net.LookupNS(host)
  18. if err != nil {
  19. fmt.Println("Error occured during NS lookup", err)
  20. }
  21. return &nserv
  22. }
  23. func main() {
  24. args()
  25. fmt.Println("Nameserver information:", host)
  26. fmt.Println(" NS records:", nslookup())
  27. }

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

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

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

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

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

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

上述代码返回如下结果:

  1. Nameserver information: google.com
  2. 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:

  1. package main
  2. import "fmt"
  3. import "net"
  4. import "os"
  5. var host string
  6. func args() {
  7. if len(os.Args) != 2 {
  8. fmt.Println("You need to enter a host!")
  9. } else {
  10. host = os.Args[1]
  11. }
  12. if host == "" {
  13. os.Exit(0)
  14. }
  15. }
  16. func nslookup() []*net.NS {
  17. nserv, err := net.LookupNS(host)
  18. if err != nil {
  19. fmt.Println("Error occured during NS lookup", err)
  20. }
  21. return *&nserv
  22. }
  23. func main() {
  24. args()
  25. fmt.Println("Nameserver information:", host)
  26. fmt.Println(" NS records:", nslookup())
  27. }

Given e.g. google.com, it displays the following:

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

Instead of the memory address locations, I would like to see the dereferenced values, e.g:

  1. 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:

  1. func nslookup() *net.NS {
  2. // The rest of the function
  3. return *&nserv[0] // This returns the first nameserver

The above returns the following:

  1. Nameserver information: google.com
  2. 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 修饰符来查看数据的实际样子。

示例:

  1. package main
  2. import "fmt"
  3. import "net"
  4. import "os"
  5. var host string
  6. func nslookupString(nserv []*net.NS) (hosts []string) {
  7. hosts = make([]string, len(nserv))
  8. for i, host := range nserv {
  9. hosts[i] = host.Host
  10. }
  11. return
  12. }
  13. func nslookupNS(host string) []*net.NS {
  14. nserv, err := net.LookupNS(host)
  15. if err != nil {
  16. fmt.Println("NS查找期间发生错误", err)
  17. }
  18. return nserv
  19. }
  20. func init() { //通常在init()中初始化全局参数
  21. if len(os.Args) == 2 {
  22. host = os.Args[1]
  23. }
  24. }
  25. func main() {
  26. if host == "" {
  27. fmt.Println("你需要输入一个主机名!")
  28. os.Exit(1)
  29. }
  30. fmt.Println("Nameserver 信息:", host)
  31. ns := nslookupNS(host)
  32. fmt.Printf(" NS 记录字符串: %#q\n", nslookupString(ns))
  33. fmt.Printf(" NS 记录 net.NS: %q\n", ns)
  34. for _, h := range ns {
  35. fmt.Printf("%#v\n", h)
  36. }
  37. }
英文:

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:

  1. package main
  2. import "fmt"
  3. import "net"
  4. import "os"
  5. var host string
  6. func nslookupString(nserv []*net.NS) (hosts []string) {
  7. hosts = make([]string, len(nserv))
  8. for i, host := range nserv {
  9. hosts[i] = host.Host
  10. }
  11. return
  12. }
  13. func nslookupNS(host string) []*net.NS {
  14. nserv, err := net.LookupNS(host)
  15. if err != nil {
  16. fmt.Println("Error occured during NS lookup", err)
  17. }
  18. return nserv
  19. }
  20. func init() { //initilizing global arguments is usually done in init()
  21. if len(os.Args) == 2 {
  22. host = os.Args[1]
  23. }
  24. }
  25. func main() {
  26. if host == "" {
  27. fmt.Println("You need to enter a host!")
  28. os.Exit(1)
  29. }
  30. fmt.Println("Nameserver information:", host)
  31. ns := nslookupNS(host)
  32. fmt.Printf(" NS records String: %#q\n", nslookupString(ns))
  33. fmt.Printf(" NS records net.NS: %q\n", ns)
  34. for _, h := range ns {
  35. fmt.Printf("%#v\n", h)
  36. }
  37. }

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:

确定