Mac安装Golang和vscode

Mac第一次安装golang和vscode一起使用,遇到了不少的坑,下面介绍一下正确的安装方式。

1、使用brew安装Golang

如果不知道brew是什么,或怎么安装请看这里 brew官网

brew install golang

安装完成后可以使用 brew info go查看详情

LiangdeMacBook-Pro:bin liangbo$ brew info go
go: stable 1.11.2 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.11.2 (9,282 files, 404MB) *
  Poured from bottle on 2018-12-03 at 17:30:25
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.10 ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
A 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#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Analytics
install: 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)

1.2 Golang环境变量

有提到环境变量,看一下go的环境变量怎么配置

  • GOPATH 工作空间目录,以我本机为例:/Users/liangbo/Documents/work/go_work
  • GOROOT GO安装目录,以上信息中为 /usr/local/opt/go/libexec

Mac环境的环境变量设置在vim ~/.bash_profile,在文件尾部添加

#GOROOT
export GOROOT=/usr/local/opt/go/libexec

#GOPATH WORKSPACE
export GOPATH=~/Documents/work/go_work

#GOROOT BIN
export PATH=$PATH:$GOROOT/bin

#GOPATH BIN
export PATH=$PATH:$GOPATH/bin

设置完成后 执行 source ~/.bash_profile 生效
查看 go env

LiangdeMacBook-Pro:go_work liangbo$ go env
GOARCH="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"

2、安装vscode

  • 官网下载安装: vscode 官网
  • brew cask 安装:
brew cask install visual-studio-code

2.2 vscode中Go的相关插件安装

由于网络原因可能无法下载的解决方案

  1. 进入cd $GOPATH/src/github.com/golang,如果没有目录就手动创建
  2. 下载插件 git clone https://github.com/golang/tools.git tools,下载完后,会多一个tools目录
git clone https://github.com/golang/tools.git tools
  1. 将tools拷贝到 $GOPATH/src/golang.org/x/tools,Mac命令如下
# 将tools目录复制到$GOPATH/src/golang.org/x下
cp -r $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/
  1. 接下来可以安装无法安装的插件,需要切换到cd $GOPATH目录,使用 go install安装:
  • go install github.com/ramya-rao-a/go-outline
  • go install golang.org/x/tools/cmd/guru
  • ......
    只要提示你哪个无法安装的就以这种形式安装
  1. golang.org/x不止tools,还有其它的工具,如果提示无法下载,还可以直接下载:
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/text.git

比如我本机上

LiangdeMacBook-Pro:x liangbo$ ls -l
total 0
drwxr-xr-x  43 liangbo  staff  1376 12  3 12:41 crypto
drwxr-xr-x  12 liangbo  staff   384 12  3 16:31 lint
drwxr-xr-x  34 liangbo  staff  1088 12  1 14:45 net
drwxr-xr-x  16 liangbo  staff   512 12  3 14:29 sys
drwxr-xr-x  33 liangbo  staff  1056 12  3 12:39 text
drwxr-xr-x  25 liangbo  staff   800 12  3 11:28 tools

基本上都是手动下载

git clone https://github.com/golang/text.git
git clone https://github.com/golang/sys.git
git clone https://github.com/golang/lint.git
git clone https://github.com/golang/crypto.git

3、安装golang debug插件delve

  • mac上不能直接使用 go get 安装,否则无法执行调试
  • 使用brew install go-delve/delve/delve 或 手动安装

使用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.

3.1 选择手动安装

需要安装Xcode命令行工具。运行以下命令安装:

xcode-select --install

创建证书

  • 打开钥匙串访问;
  • 菜单栏中选择钥匙串访问-证书助理-创建证书开始创建自签名证书;
  • 证书名称设置为dlv-cert(记住这个名字,后面会用到);身份类型选择自签名根证书;证书类型选择代码签名,最后在让我覆盖这些默认值处打上勾,选择继续;
  • 在接下来的窗口中把有效期改长一些,例如改成10年(3650天);
  • 然后一直往后,直到出现选择指定用于该证书的位置,选择钥匙串系统,然后选择创建;
  • 这样证书就创建好了。

3.2 下载源码

进入 $GOPATH/src/github.com/derekparker,或许你已经使用go get 安装过delve,那么这个目录已经存在了,如果没有的话需要创建目录

  • 假如存在的话直接进入
cd $GOPATH/src/github.com/derekparker/delve
git pull origin master # 拉取最新

假如不存在的话

mkdir -p $GOPATH/src/github.com/derekparker
cd $GOPATH/src/github.com/derekparker
git clone https://github.com/derekparker/delve.git
cd delve

以上两种情况下,最后执行 CERT=dlv-cert make install

[LiangdeMacBook-Pro:delve liangbo$ CERT=dlv-cert make install
go install "-ldflags=-s -X main.Build=34e802a42b9dc3ba3c4a513b50e9eec6afedcdf2" github.com/derekparker/delve/cmd/dlv

4、vscode测试

go项目必须以工作空间方式打开


执行调试


4.1 Launch.json

{
    // 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
        }
    ]
}
  • name 随意配置;
  • port 只要不是常用的端口,应该就不会存在冲突了,随意配置;
  • program 配置Go项目启动文件路径,即main函数所在的.go文件的路径,${workspaceRoot}代表项目的根目录,也就是 /bin /pkg /src这三个文件夹所在的目录;
  • env 可以用来配置调试启动时所用的环境变量参数,比如gopath临时设置为某个参数就可以在这里指定,如果有多个gopath,用英文冒号:来连接多个gopath;
  • showLog true表示在控制台显示日志
  • trace 可以打印Delve执行的日志,在控制台会输出这个日志的目录

https://golang.org/doc/code.html#GOPATH

你可能感兴趣的:(Mac安装Golang和vscode)