什么'关键,好的:= k。(* dns.A)'在Go中意味着什么?

时间:2016-07-28 00:23:04

标签: go syntax

我是Go的新手并尝试使用包 miekg DNS编写DNS服务器。根据{{​​3}},我复制&粘贴一个简单的片段来执行A记录请求:

package main

import "fmt"
import "github.com/miekg/dns"

func main() {
    config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
    c := new(dns.Client)
    m := new(dns.Msg)
    zone := "miek.nl"
    m.SetQuestion(dns.Fqdn(zone), dns.TypeA)
    m.SetEdns0(4096, true)
    r, _, err := c.Exchange(m, config.Servers[0]+":"+config.Port)
    if err != nil {
        return
    }
    if r.Rcode != dns.RcodeSuccess {
        return
    }
    _ = "breakpoint"
    for _, k := range r.Answer {
        if key, ok := k.(*dns.A); ok {
            fmt.Printf("%+v\n", key)
        }
    }
}

由godebug加载,停在_ = "breakpoint"

./godebug run ~/aRequest.go  -d /usr/local/go/
-> _ = "breakpoint"
(godebug) p r.Answer
[]dns.RR{(*dns.A)(0xc8200120c0)}
(godebug) n
-> for _, k := range r.Answer {
(godebug) n
-> if key, ok := k.(*dns.A); ok {
(godebug) p k
&dns.A{Hdr:dns.RR_Header{Name:"miek.nl.", Rrtype:0x1, Class:0x1, Ttl:0x708, Rdlength:0x4}, A:net.IP{0x8b, 0xa2, 0xc4, 0x4e}}
(godebug) n
-> fmt.Printf("%+v\n", key)
(godebug) p key 
&dns.A{Hdr:dns.RR_Header{Name:"miek.nl.", Rrtype:0x1, Class:0x1, Ttl:0x708, Rdlength:0x4}, A:net.IP{0x8b, 0xa2, 0xc4, 0x4e}}
(godebug) p ok
true

根据godebug,ok == truekey似乎与k相同。 k.(*dns.A)如何返回两个变量?

1 个答案:

答案 0 :(得分:2)

表达式class User before_destroy :destroy_validation, if: :some_reason private def destroy_validation throw(:abort) if some_condition end type assertion

代码断言k.(*dns.A)包含k。第一个返回值是*dns.A类型。第二个返回值是一个无类型的bool,指示断言是否成立。