英文:
Get domain name from IP address in Go
问题
我正在尝试从IP地址解析主机名,这似乎比我想象的要困难一些。
我尝试使用了几个函数,包括net.LookupHost
方法,但它们似乎只返回我输入的IP地址。
这是我正在使用的代码:
package main
import (
"fmt"
"net"
)
func main() {
// 从ping -c 1 stackoverflow.com获取,应该打印出"stackoverflow.com"
addr, err := net.LookupHost("198.252.206.16")
fmt.Println(addr, err)
}
英文:
I'm trying to resolve the host name from an IP address, which is apparently proving to be a little more challenging than I thought it'd be.
I've tried using a couple of functions, including the net.LookupHost
method, but all of them seem to be just returning the IP address which I input.
Here's the code which I am using:
package main
import (
"fmt"
"net"
)
func main() {
// obtained from ping -c 1 stackoverflow.com, should print "stackoverflow.com"
addr, err := net.LookupHost("198.252.206.16")
fmt.Println(addr, err)
}
答案1
得分: 21
例如,
package main
import (
"fmt"
"net"
)
func main() {
// 从 ping -c 1 stackoverflow.com 获取,应该打印 "stackoverflow.com"
addr, err := net.LookupAddr("198.252.206.16")
fmt.Println(addr, err)
}
输出:
[stackoverflow.com.] <nil>
英文:
For example,
package main
import (
"fmt"
"net"
)
func main() {
// obtained from ping -c 1 stackoverflow.com, should print "stackoverflow.com"
addr, err := net.LookupAddr("198.252.206.16")
fmt.Println(addr, err)
}
Output:
[stackoverflow.com.] <nil>
答案2
得分: 4
您需要使用LookupAddr而不是LookupHost。
英文:
You need LookupAddr instead of LookupHost.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论