从MacOS goland无法debug到dns无法解析localhost

背景

MacOS 13.0.1

GoLand2022.2

GoLand开发golang项目,可以run,但是无法debug...

复制代码
​
/Applications/GoLand.app/Contents/plugins/go/lib/dlv/mac/dlv --listen=10.xx.xxx.129:63466 --headless=true --api-version=2 --check-go-version=false --only-same-user=false exec /private/var/folders/7p/dsbvg0k54139kxmr4fyd864w0000gn/T/GoLand/___go_build_codeup_aliyun_com_607fcd17c06031c0ebd327fc_canopia_be_business_cmd_rpc 

couldn't start listener: listen tcp 10.xx.xxx.129:63466: bind: can't assign requested address
Debugger finished with the exit code 1

​

解决思路

  1. debug无法找到正确的本地IP地址,尝试修改 /etc/hosts 强行指定

  2. goland dlv 强行指定debug进程启动时候的listen地址为127.0.0.1 (无法配置)

分析

1.日志中出现的IP,可以发现是远程dns的服务器VIP(多个ip轮巡)

2.测试 `ping localhost` ,发现解析出来的地址不是 127.0.0.1 ,还是远程dns的ip

3.尝试通过修改 /etc/hosts 和 /etc/resolv.conf 文件

  • /etc/hosts 理论上是优先级最高的配置,但是可能被一些信息安全工具修改

  • /etc/resolv.conf 一般自动修改,不需要手动,但是也可能被工具修改

尝试手动修改2个文件,发现还是不生效

4.尝试清理dns缓存,保证 /etc/hosts 生效

复制代码
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

清理后并没有效果...

5.尝试在 /etc/hosts 文件中配置其他,比如

复制代码
10.xx.xx.104 github.com

ping github.com 的时候无法解析到 10.xx.xx.104

6.问题逐渐变成 /etc/hosts 为什么不生效?

7.Google & chatgpt 查询,得到了一些可能的问题

  • 需要保证 /etc/hosts 是属于 root 用户 wheel 组,并且权限是644

    sudo ls -l /etc/hosts

    -rw-r--r--@ 1 root wheel 2110 8 7 22:02 /etc/hosts

  • 保证mac的网络配置是关闭自动ipv6的,使用仅本地连接

  • 某些操作系统版本,严格限制 /etc/hosts 内容的格式(或者说是bug),才会生效

要求key value之间只能有一个空格...或者tab(一个或者多个),无法混用

复制代码
# 每行的key value只能有一个空格,或者一个/多个 tab
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1	localhost

解决

上面分析中需要保证的部分全部修改,之后 /etc/hosts 立刻生效;

结论:

  1. goland dlv启动本地debug进程默认是localhost,并且无法修改

2./etc/hosts 文件修改是立刻生效的,如果不生效,说明文件本身存在问题

相关推荐
比特森林探险记1 天前
go 语言中的context 解读和用法
开发语言·后端·golang
liyoro1 天前
用 Codex + 提示词生成一个快速打开 Ghostty 的 macOS 小工具
macos·shell·ai编程
jieyucx2 天前
从基础语法到面向对象:Go语言如何实现封装、继承与多态?
开发语言·后端·golang
littleschemer2 天前
Go:实现游戏服务器网关
服务器·网关·游戏·golang
A懿轩A2 天前
Claude Code 2026 最新版下载安装教程详细版 涵盖Windows 和 MacOS 安装 附常见问题解决方案
windows·macos
倔强的石头1062 天前
SenseNova-U1 实战体验:从网页版生成,到 Mac 踩坑,再到 CUDA 服务器跑通本地部署
运维·服务器·macos
念何架构之路2 天前
Go依赖管理
开发语言·后端·golang
必胜刻2 天前
Go 调用Coze工作流实现 AI 游戏生成
开发语言·ai·golang·gin
zandy10113 天前
2026 主流技术栈:hermes agent多环境安装配置:Windows/Mac/Linux
linux·windows·macos
2501_916008893 天前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview