Mac第一次安装golang和vscode一起使用,遇到了不少的坑,下面介绍一下正确的安装方式。
如果不知道brew是什么,或怎么安装请看这里 brew官网
brew install golang
安装完成后可以使用 brew info go查看详情
LiangdeMacBook-Pro:bin liangbo$ brew info gogo: stable 1.11.2 (bottled), HEADOpen source programming language to build simple/reliable/efficient softwarehttps://golang.org/usr/local/Cellar/go/1.11.2 (9,282 files, 404MB) * Poured from bottle on 2018-12-03 at 17:30:25From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb==> RequirementsRequired: macOS >= 10.10 ✔==> Options--HEAD Install HEAD version==> CaveatsA valid GOPATH is required to use the `go get` command.If $GOPATH is not specified, $HOME/go will be used by default: https://golang.org/doc/code.html#GOPATHYou may wish to add the GOROOT-based install location to your PATH: export PATH=$PATH:/usr/local/opt/go/libexec/bin==> Analyticsinstall: 90,997 (30 days), 251,942 (90 days), 954,291 (365 days)install_on_request: 64,631 (30 days), 178,671 (90 days), 610,317 (365 days)build_error: 0 (30 days)
有提到环境变量,看一下go的环境变量怎么配置
Mac环境的环境变量设置在vim ~/.bash_profile,在文件尾部添加
#GOROOTexport GOROOT=/usr/local/opt/go/libexec#GOPATH WORKSPACEexport GOPATH=~/Documents/work/go_work#GOROOT BINexport PATH=$PATH:$GOROOT/bin#GOPATH BINexport PATH=$PATH:$GOPATH/bin
设置完成后 执行 source ~/.bash_profile 生效查看 go env
LiangdeMacBook-Pro:go_work liangbo$ go envGOARCH="amd64"GOBIN=""GOCACHE="/Users/liangbo/Library/Caches/go-build"GOEXE=""GOFLAGS=""GOHOSTARCH="amd64"GOHOSTOS="darwin"GOOS="darwin"GOPATH="/Users/liangbo/Documents/work/go_work"GOPROXY=""GORACE=""GOROOT="/usr/local/Cellar/go/1.11.2/libexec"GOTMPDIR=""GOTOOLDIR="/usr/local/Cellar/go/1.11.2/libexec/pkg/tool/darwin_amd64"GCCGO="gccgo"CC="clang"CXX="clang++"CGO_ENABLED="1"GOMOD=""CGO_CFLAGS="-g -O2"CGO_CPPFLAGS=""CGO_CXXFLAGS="-g -O2"CGO_FFLAGS="-g -O2"CGO_LDFLAGS="-g -O2"PKG_CONFIG="pkg-config"GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4t/rrdtjpp9487_6gf8mclrslpm0000gn/T/go-build714070253=/tmp/go-build -gno-record-gcc-switches -fno-common"
brew cask install visual-studio-code
由于网络原因可能无法下载的解决方案
git clone https://github.com/golang/tools.git tools
# 将tools目录复制到$GOPATH/src/golang.org/x下cp -r $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/xgit clone https://github.com/golang/text.git
比如我本机上
LiangdeMacBook-Pro:x liangbo$ ls -ltotal 0drwxr-xr-x 43 liangbo staff 1376 12 3 12:41 cryptodrwxr-xr-x 12 liangbo staff 384 12 3 16:31 lintdrwxr-xr-x 34 liangbo staff 1088 12 1 14:45 netdrwxr-xr-x 16 liangbo staff 512 12 3 14:29 sysdrwxr-xr-x 33 liangbo staff 1056 12 3 12:39 textdrwxr-xr-x 25 liangbo staff 800 12 3 11:28 tools
基本上都是手动下载
git clone https://github.com/golang/text.gitgit clone https://github.com/golang/sys.gitgit clone https://github.com/golang/lint.gitgit clone https://github.com/golang/crypto.git
使用brew 安装找不到包,不太清楚原因
Error: No available formula with the name "go-delve/delve/delve" ==> Searching for a previously deleted formula (in the last month)...Error: No previously deleted formula found.==> Searching for similarly named formulae...Error: No similarly named formulae found.
需要安装Xcode命令行工具。运行以下命令安装:
xcode-select --install
创建证书
进入 $GOPATH/src/github.com/derekparker,或许你已经使用go get 安装过delve,那么这个目录已经存在了,如果没有的话需要创建目录
cd $GOPATH/src/github.com/derekparker/delvegit pull origin master # 拉取最新
假如不存在的话
mkdir -p $GOPATH/src/github.com/derekparkercd $GOPATH/src/github.com/derekparkergit clone https://github.com/derekparker/delve.gitcd delve
以上两种情况下,最后执行 CERT=dlv-cert make install
[LiangdeMacBook-Pro:delve liangbo$ CERT=dlv-cert make installgo install "-ldflags=-s -X main.Build=34e802a42b9dc3ba3c4a513b50e9eec6afedcdf2" github.com/derekparker/delve/cmd/dlv
go项目必须以工作空间方式打开
执行调试
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Launch", // 名称可以随意写 "type": "go", "request": "launch", "mode": "auto", "program": "${fileDirname}", "env": {}, "args": [], "showLog": true, "host": "127.0.0.1", "port": 23456, "trace": true } ]}