macos M1 芯片,無法使用 vscode 調試 golang


新購的 macbook,芯片為 Apple M1 Pro
今天使用 vscode 開發 golang項目,想要使用debug功能進行調試,結果出現如下問題:

Failed to launch: could not launch process: can not run under Rosetta, check that the installed build of Go is right for your CPU architecture

幾番搜索,終於在 stackoverflow 找到解決方案:Cannot run debug Go using VSCode on Mac M1

整理如下:

1.

Apple M1 Pro 芯片是arm架構的,將本機的 go 環境更換為 darwin-arm64 架構的
https://go.dev/dl/

2.

進入 $GOPATH 目錄,將 bin 文件夾 rm 掉,或者 mv 更新名稱;然后新建 bin 目錄。
vscode 使用 $GOPATH/bin/dlv 進行golang項目的調試,之前由於安裝的 go 是 darwin-amd64 架構的,所以之后由vscode安裝的 dlv 也是 darwin-amd64 架構的了。其實 $GOPATH/bin 目錄中的 tools 都是 vscode 使用 go 編譯生成的。如果 go 本身是 darwin-amd64 架構的,那么編譯出來的 bin 文件自然也是 darwin-amd64 架構的了。

3.

退出 vscode,再重新打開,這時候 vscode 會有彈窗表示 $GOPATH/bin 目錄下缺少各種 tools 文件,直接點擊 Install All 就可以了。
或者按 Shift + Command + P,打開 vscode 命令輸入框,輸入 go: install tools,選擇 Go: Install/Update Tools 即可

end.

然后就可以正常調試了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM