本篇介紹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
- 打開