Home >Backend Development >Golang >GoLang: net.LookupHost returns duplicate ip
php editor Xigua today will introduce to you a function net.LookupHost in the Go language. This function may return duplicate IP addresses when performing host name resolution. In practical applications, this may cause problems such as connecting to the same server multiple times. Therefore, we need to understand the cause and solution of this problem to ensure the correctness and stability of the code. In the following article, we will analyze this problem in detail and provide corresponding solutions. stay tuned!
I'm trying to run net.lookuphost to get the ip address of the computer running my code. It works on most machines, but on a few it returns a slice with two entries: the correct ip and the duplicate entry with the same ip.
I tried running nslookup on the host but it only returned one entry.
Add code to question -
hostname, _ := os.Hostname() ips, err := net.LookupHost(hostname) if err != nil { fmt.Printf("%d\n", err.Error()) } fmt.Printf("Hello, 世界 %#v \n", ips) ips2, err := net.LookupIP(hostname) if err != nil { fmt.Printf("%d\n", err.Error()) } fmt.Printf("Hello, 世界 %#v \n", ips2)
I expected net.lookuphost to give only one entry in the response, but it gave two identical entries in the response.
Looks like a DNS resolution issue.
If it works on other servers, try checking the file /etc/hosts
.
The system first queries the /etc/hosts file, and then queries the DNS server.
Source: https://debian-handbook.info /browse/stable/sect.hostname-name-service.html
The above is the detailed content of GoLang: net.LookupHost returns duplicate ip. For more information, please follow other related articles on the PHP Chinese website!