本篇介紹Mac
平臺 使用visual studio code
搭建golang
開發環境以及debug
環境的配置git
# 安裝 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
GOROOT
: 安裝目錄GOPATH
: 工做空間目錄 (該目錄不能和Go的安裝目錄同樣,這個目錄下面有三個子目錄: src、bin、pkg
)
Mac環境的環境變量設置在vim ~/.bash_profile
,在文件尾部添加github
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
檢查環境變量是否設置成功golang
$ 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"
# 安裝: 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)
go
: 開發vscode-icons
: 顏值, 彩色文件,目錄Material Theme Kit
: 編輯器 UI 主題visual studio code settings sync
: 同步 vscode 配置文件自定義vscodevim
{ "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 }
因爲網絡緣由可能沒法下載(沒法上外網)的解決方案:visual-studio-code
cd $GOPATH/src/github.com/golang
,若是沒有請建立下載tools
工具:xcode
git clone https://github.com/golang/tools.git tools
將tools
拷貝到 $GOPATH/src/golang.org/x/tools
bash
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命令行工具安裝app
xcode-select --install
建立證書編輯器
鑰匙串訪問
;鑰匙串訪問
-證書助理
-建立證書
開始建立自簽名證書
;讓我覆蓋這些默認值處
打上勾,選擇繼續;這樣證書就建立好了。
進入$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