msg := new(dns.Msg) msg.SetQuestion("example.com.", dns.TypeA) msg.RecursionDesired = true
conn, err := net.Dial("tcp", "dns.server.com:53") if err != nil { log.Fatalf("failed to connect to server: %v", err) } defer conn.Close() msg := new(dns.Msg) msg.SetQuestion("example.com.", dns.TypeA) msg.RecursionDesired = true buf, err := msg.Pack() if err != nil { log.Fatalf("failed to serialize message: %v", err) } if err := binary.Write(conn, binary.BigEndian, uint16(len(buf))); err != nil { log.Fatalf("failed to send message length: %v", err) } if _, err := conn.Write(buf); err != nil { log.Fatalf("failed to send message: %v", err) }This creates a TCP connection to the DNS server `dns.server.com` on port 53. It then creates a `Msg` object asking for the IPv4 address of `example.com` with the `RecursionDesired` flag set to true. The `Pack` method is used to serialize the message into a binary format, which is then sent over the TCP connection as a series of packets. The length of the message is sent first as a 16-bit integer in network byte order, followed by the serialized message. In conclusion, the `github.com/miekg/dns` package provides Go developers with a powerful toolset for working with the DNS protocol. With the `Msg` struct, developers can easily create and manipulate DNS messages, including setting the `RecursionDesired` flag to control how DNS queries are processed.