本篇介绍Mac
平台 使用visual studio code
搭建golang
开发环境以及debug
环境的配置
1. brew安装golang
# 安装
brew install golang
# 查看是否安装成功
$ go version
go version go1.12.7 darwin/amd64
$ brew info go
go: stable 1.13.3 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.12.7 (9,816 files, 452.7MB) *
Poured from bottle on 2019-07-16 at 22:22:49
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.10 ✔
==> Options
--HEAD
Install HEAD version
2. 配置Golang环境变量
GOROOT
: 安装目录GOPATH
: 工作空间目录 (该目录不能和Go的安装目录一样,这个目录下面有三个子目录: src、bin、pkg
)
Mac环境的环境变量设置在vim ~/.bash_profile
,在文件尾部添加
export GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
export GOPATH="${HOME}/Documents/go"
export PATH="${PATH}:${GOPATH}:${GOPATH}/bin"
设置完成后 执行source ~/.bash_profile
生效
查看go env
检查环境变量是否设置成功
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xxxx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xxxx/Documents/work/go_work"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/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"
3. 安装vscode
- 官网下载安装:vscode 官网
- brew cask 安装:
# 安装:
brew cask install visual-studio-code
# 查看 vscode 信息:
-> % brew cask info visual-studio-code
visual-studio-code: 1.3.1,e6b4afa53e9c0f54edef1673de9001e9f0f547ae
Microsoft Visual Studio Code, VS Code
https://code.visualstudio.com/
Not installed
https://github.com/caskroom/homebrew-cask/blob/master/Casks/visual-studio-code.rb
==> Contents
Visual Studio Code.app (app)
/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code (binary)
- vscode插件安装
go
: 开发vscode-icons
: 颜值, 彩色文件,目录Material Theme Kit
: 编辑器 UI 主题visual studio code settings sync
: 同步 vscode 配置文件
30个极大提高开发效率的VSCode插件 -
自定义vscode
{ "files.autoSave": "afterDelay", "editor.fontSize": 14, // 以像素为单位控制字号。 "terminal.external.osxExec": "iterm.app", // 自定义要在 OS X 上运行的终端应用程序 "go.gopath": "/Users/xxxx/", // 个人的 GOPATH个人开发工作区 "go.goroot": "/usr/local/Cellar/go/1.7.3/libexec", //GO安装目录 "workbench.statusBar.visible": false }
4. 使用 vscode 自动安装 debug 必备的插件
由于网络原因可能无法下载(无法上外网)的解决方案:
- 进入
cd $GOPATH/src/github.com/golang
,如果没有请创建 -
下载
tools
工具:git clone https://github.com/golang/tools.git tools
-
将
tools
拷贝到$GOPATH/src/golang.org/x/tools
cp -r $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/
-
安装
golang debug
插件delve
使用brew install go-delve/delve/delve
或 手动安装
但实际中发现会出现错误,找不到安装包,不知如何解决,改手动安装Updating Homebrew... Error: No available formula with the name "go-delve/delve/delve" ==> Searching for a previously deleted formula (in the last month)... Warning: go-delve/delve is shallow clone. To get complete history run: git -C "$(brew --repo go-delve/delve)" fetch --unshallow Error: No previously deleted formula found. ==> Searching for similarly named formulae... Error: No similarly named formulae found.
使用Xcode命令行工具安装
xcode-select --install
创建证书
- 打开
钥匙串访问
; - 菜单栏中选择
钥匙串访问
-证书助理
-创建证书
开始创建自签名证书
; - 证书名称设置为dlv;身份类型选择自签名根证书;证书类型选择代码签名,最后在
让我覆盖这些默认值处
打上勾,选择继续; - 有效期改长一些;
- 然后一直往后,直到出现选择指定用于该证书的位置,选择钥匙串系统,然后选择创建;
-
这样证书就创建好了。
进入
$GOPATH/src/github.com/derekparker
,同样,如果没有请自行创建目录cd $GOPATH/src/github.com/derekparker git clone https://github.com/derekparker/delve.git cd delve # 安装 CERT=dlv make install go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/golang/lint/golint go get -u -v github.com/lukehoban/go-outline go get -u -v sourcegraph.com/sqs/goreturns go get -u -v golang.org/x/tools/cmd/gorename go get -u -v github.com/tpng/gopkgs go get -u -v github.com/newhook/go-symbols go get -u -v golang.org/x/tools/cmd/guru
- 打开
有疑问加站长微信联系(非本文作者)