从Go中的IP地址获取域名 [英] Get domain name from IP address in Go

查看:113
本文介绍了从Go中的IP地址获取域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从IP地址中解析主机名,这显然比我想象的要好一些。

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.

我已经尝试使用了一些功能,包括 net.LookupHost 方法,但是它们似乎只是返回我输入的IP地址。

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.

这是我使用的代码:

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)
}


推荐答案

例如,

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)
}

输出:

[stackoverflow.com.] <nil>

这篇关于从Go中的IP地址获取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆