Mac第一次安装golang和vscode一起使用,遇到了不少的坑,下面介绍一下正确的安装方式。
1、使用brew安装Golang
如果不知道brew是什么,或怎么安装请看这里 brew官网
brew install golang
安装完成后可以使用 brew info go查看详情
1LiangdeMacBook-Pro:bin liangbo$ brew info go 2go: stable 1.11.2 (bottled), HEAD 3Open source programming language to build simple/reliable/efficient software 4https://golang.org 5/usr/local/Cellar/go/1.11.2 (9,282 files, 404MB) * 6 Poured from bottle on 2018-12-03 at 17:30:25 7From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb 8==> Requirements 9Required: macOS >= 10.10 ✔ 10==> Options 11--HEAD 12 Install HEAD version 13==> Caveats 14A valid GOPATH is required to use the `go get` command. 15If $GOPATH is not specified, $HOME/go will be used by default: 16 https://golang.org/doc/code.html#GOPATH 17 18You may wish to add the GOROOT-based install location to your PATH: 19 export PATH=$PATH:/usr/local/opt/go/libexec/bin 20==> Analytics 21install: 90,997 (30 days), 251,942 (90 days), 954,291 (365 days) 22install_on_request: 64,631 (30 days), 178,671 (90 days), 610,317 (365 days) 23build_error: 0 (30 days)
1.2 Golang环境变量
有提到环境变量,看一下go的环境变量怎么配置
- GOPATH 工作空间目录,以我本机为例:
/Users/liangbo/Documents/work/go_work - GOROOT GO安装目录,以上信息中为
/usr/local/opt/go/libexec
Mac环境的环境变量设置在vim ~/.bash_profile,在文件尾部添加
1#GOROOT 2export GOROOT=/usr/local/opt/go/libexec 3 4#GOPATH WORKSPACE 5export GOPATH=~/Documents/work/go_work 6 7#GOROOT BIN 8export PATH=$PATH:$GOROOT/bin 9 10#GOPATH BIN 11export PATH=$PATH:$GOPATH/bin
设置完成后 执行 source ~/.bash_profile 生效
查看 go env
1LiangdeMacBook-Pro:go_work liangbo$ go env 2GOARCH="amd64" 3GOBIN="" 4GOCACHE="/Users/liangbo/Library/Caches/go-build" 5GOEXE="" 6GOFLAGS="" 7GOHOSTARCH="amd64" 8GOHOSTOS="darwin" 9GOOS="darwin" 10GOPATH="/Users/liangbo/Documents/work/go_work" 11GOPROXY="" 12GORACE="" 13GOROOT="/usr/local/Cellar/go/1.11.2/libexec" 14GOTMPDIR="" 15GOTOOLDIR="/usr/local/Cellar/go/1.11.2/libexec/pkg/tool/darwin_amd64" 16GCCGO="gccgo" 17CC="clang" 18CXX="clang++" 19CGO_ENABLED="1" 20GOMOD="" 21CGO_CFLAGS="-g -O2" 22CGO_CPPFLAGS="" 23CGO_CXXFLAGS="-g -O2" 24CGO_FFLAGS="-g -O2" 25CGO_LDFLAGS="-g -O2" 26PKG_CONFIG="pkg-config" 27GOGCCFLAGS="-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"
2、安装vscode
- 官网下载安装: vscode 官网
- brew cask 安装:
brew cask install visual-studio-code
2.2 vscode中Go的相关插件安装
由于网络原因可能无法下载的解决方案
- 进入
cd $GOPATH/src/github.com/golang,如果没有目录就手动创建 - 下载插件 git clone https://github.com/golang/tools.git tools,下载完后,会多一个tools目录
git clone https://github.com/golang/tools.git tools
- 将tools拷贝到 $GOPATH/src/golang.org/x/tools,Mac命令如下
1# 将tools目录复制到$GOPATH/src/golang.org/x下 2cp -r $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/
- 接下来可以安装无法安装的插件,需要切换到cd $GOPATH目录,使用
go install安装:
- go install github.com/ramya-rao-a/go-outline
- go install golang.org/x/tools/cmd/guru
- ......
只要提示你哪个无法安装的就以这种形式安装
- golang.org/x不止tools,还有其它的工具,如果提示无法下载,还可以直接下载:
1cd $GOPATH/src/golang.org/x 2git clone https://github.com/golang/text.git
比如我本机上
1LiangdeMacBook-Pro:x liangbo$ ls -l 2total 0 3drwxr-xr-x 43 liangbo staff 1376 12 3 12:41 crypto 4drwxr-xr-x 12 liangbo staff 384 12 3 16:31 lint 5drwxr-xr-x 34 liangbo staff 1088 12 1 14:45 net 6drwxr-xr-x 16 liangbo staff 512 12 3 14:29 sys 7drwxr-xr-x 33 liangbo staff 1056 12 3 12:39 text 8drwxr-xr-x 25 liangbo staff 800 12 3 11:28 tools
基本上都是手动下载
1git clone https://github.com/golang/text.git 2git clone https://github.com/golang/sys.git 3git clone https://github.com/golang/lint.git 4git clone https://github.com/golang/crypto.git
3、安装golang debug插件delve
- mac上不能直接使用 go get 安装,否则无法执行调试
- 使用brew install go-delve/delve/delve 或 手动安装
使用brew 安装找不到包,不太清楚原因
1Error: No available formula with the name "go-delve/delve/delve" 2==> Searching for a previously deleted formula (in the last month)... 3Error: No previously deleted formula found. 4==> Searching for similarly named formulae... 5Error: No similarly named formulae found.
3.1 选择手动安装
需要安装Xcode命令行工具。运行以下命令安装:
xcode-select --install
创建证书
- 打开钥匙串访问;
- 菜单栏中选择钥匙串访问-证书助理-创建证书开始创建自签名证书;
- 证书名称设置为dlv-cert(记住这个名字,后面会用到);身份类型选择自签名根证书;证书类型选择代码签名,最后在让我覆盖这些默认值处打上勾,选择继续;
- 在接下来的窗口中把有效期改长一些,例如改成10年(3650天);
- 然后一直往后,直到出现选择指定用于该证书的位置,选择钥匙串系统,然后选择创建;
- 这样证书就创建好了。
3.2 下载源码
进入 $GOPATH/src/github.com/derekparker,或许你已经使用go get 安装过delve,那么这个目录已经存在了,如果没有的话需要创建目录
- 假如存在的话直接进入
1cd $GOPATH/src/github.com/derekparker/delve 2git pull origin master # 拉取最新
假如不存在的话
1mkdir -p $GOPATH/src/github.com/derekparker 2cd $GOPATH/src/github.com/derekparker 3git clone https://github.com/derekparker/delve.git 4cd delve
以上两种情况下,最后执行 CERT=dlv-cert make install
1[LiangdeMacBook-Pro:delve liangbo$ CERT=dlv-cert make install 2go install "-ldflags=-s -X main.Build=34e802a42b9dc3ba3c4a513b50e9eec6afedcdf2" github.com/derekparker/delve/cmd/dlv
4、vscode测试
go项目必须以工作空间方式打开
执行调试
4.1 Launch.json
1{ 2 // Use IntelliSense to learn about possible attributes. 3 // Hover to view descriptions of existing attributes. 4 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 "version": "0.2.0", 6 "configurations": [ 7 { 8 "name": "Launch", // 名称可以随意写 9 "type": "go", 10 "request": "launch", 11 "mode": "auto", 12 "program": "${fileDirname}", 13 "env": {}, 14 "args": [], 15 "showLog": true, 16 "host": "127.0.0.1", 17 "port": 23456, 18 "trace": true 19 } 20 ] 21}
- name 随意配置;
- port 只要不是常用的端口,应该就不会存在冲突了,随意配置;
- program 配置Go项目启动文件路径,即main函数所在的.go文件的路径,${workspaceRoot}代表项目的根目录,也就是 /bin /pkg /src这三个文件夹所在的目录;
- env 可以用来配置调试启动时所用的环境变量参数,比如gopath临时设置为某个参数就可以在这里指定,如果有多个gopath,用英文冒号:来连接多个gopath;
- showLog true表示在控制台显示日志
- trace 可以打印Delve执行的日志,在控制台会输出这个日志的目录
