有什么方法可以告诉DNS解析程序只查找IPV4地址吗



我试图配置一个Go程序(即Prometheus(,使其只查找IPv4地址,但该程序用于DNS解析的LookupIP函数似乎没有此选项。

我有什么办法可以解决这个问题吗?还是我做错了什么?LookupIP函数与src:中的函数相同

// LookupIP looks up a host using the local resolver.
// It returns a slice of that host's IPv4 and IPv6 addresses.
func LookupIP(host string) ([]IP, error) {
addrs, err := DefaultResolver.LookupIPAddr(context.Background(), host)
if err != nil {
return nil, err
}
ips := make([]IP, len(addrs))
for i, ia := range addrs {
ips[i] = ia.IP
}
return ips, nil
}

我想您正在寻找LookupIP。这是相同的功能,但你通过的ip网络你的目标。

// LookupIP looks up host for the given network using the local resolver.
// It returns a slice of that host's IP addresses of the type specified by
// network.
// network must be one of "ip", "ip4" or "ip6".
func (r *Resolver) LookupIP(ctx context.Context, network, host string) ([]IP, error) {

用法:


net.DefaultResolver.LookupIP(context.Background(), "ip4", "host")

最新更新