


Can Go\'s Standard Library Specify a Custom DNS Server for Lookup Functions?
Nov 29, 2024 pm 09:58 PMCan Go's Lookup Functions Specify a Server?*
Go's dnsclient library seamlessly loads configuration from /etc/resolv.conf, providing essential DNS functionality. However, certain scenarios demand the utilization of a specific DNS server. Does the Go standard library offer a mechanism akin to:
func LookupTXT(name string, dnsServer string) (txt []string, err error)
that can override the default DNS server while maintaining the integrity of /etc/resolv.conf?
Solution
Although the DNS client provided by Go does not directly support specifying a server during DNS resolution, there are alternative approaches to achieve this functionality. One viable option is to leverage the comprehensive dns library developed by miekg:
import ( "log" "github.com/miekg/dns" ) func main() { target := "microsoft.com" server := "8.8.8.8" c := dns.Client{} m := dns.Msg{} m.SetQuestion(target+".", dns.TypeA) r, t, err := c.Exchange(&m, server+":53") if err != nil { log.Fatal(err) } log.Printf("Took %v", t) if len(r.Answer) == 0 { log.Fatal("No results") } for _, ans := range r.Answer { Arecord := ans.(*dns.A) log.Printf("%s", Arecord.A) } }
Upon executing this code, you will obtain the resolved IP addresses for the target domain, demonstrating the effective use of a specified DNS server without compromising the system's default configuration.
The above is the detailed content of Can Go\'s Standard Library Specify a Custom DNS Server for Lookup Functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How do I write mock objects and stubs for testing in Go?

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications?

How to write files in Go language conveniently?
