doushi3454 2018-09-01 19:11
浏览 166

使用os.Hostname()后,net.LookUpHost()不输出任何此类主机

I'm currently attempting to learn the programming language go and stubbled across this weird problem. I've created a simple go application that gets the Hostname succesfully, which I then try to get my addresses with. Finally I iterate over these addresses.

package main

import (
    "fmt"
    "net"
    "os"
    "strconv"
)

func main() {
    name, error := os.Hostname()
    fmt.Println("Name: " + name)
    fmt.Print("Error: ")
    fmt.Println(error)

    addrs, err := net.LookupHost(name)

    fmt.Println(addrs)

    fmt.Println(err)

    for indx, addr := range addrs {
        fmt.Println("Address number " + strconv.Itoa(indx) + ": " + addr)
    }
}

The output of this is the following:

Name: My-macbook.local
Error: <nil>
[]
lookup My-macbook.local: no such host

I also tried using net.LookUpIP(name), which resulted in the same. I'm connected to the internet and I checked that I had an ip address in system preferences -> network. I am currently on a Macbook Pro with macOS Sierra.

My friend ran the exact same code with the exact same version of go and had two addresses returned so do I have something preventing me from seeing my Ip addresses. Has anyone experienced this before?

  • 写回答

1条回答 默认 最新

  • donglie9067 2018-09-01 19:55
    关注

    The .local address you are trying to resolve is not a regular DNS entry, its a special one for local networks. It has several names such as bonjour, avahi etc. The RFC calls it mdns https://en.wikipedia.org/wiki/.local, see also dns service discovery.

    According to the doc you should try to set this environment variable export GODEBUG=netdns=cgo in order to force the golang dns resolver to call for the system libraries. Indeed the pure golang DNS resolver does not resolve mdns.

    You could also try an alternate mdns golang implementation such as https://github.com/hashicorp/mdns

    Note also that the net package provides a DefaultResolver variable that you may override with a custom implementation to resolve \.local$.

    评论

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!