Go是一个由Google团队和开源社区的许多贡献者开发的开源项目. 本文详细记录了Go开发环境搭建过程,包括Go安装,IDE配置等。
系统环境配置
MacOS High Sierra 版本10.13.5 MacBook Pro (Retina, 15-inch, Mid 2015) 处理器 2.2 GHz Intel Core i7 内存 16 GB 1600 MHz DDR3 图形卡 Intel Iris Pro 1536 MB
Go官网提供了可用的Go列表,可以直接下载安装。本文主要采用了homebrew工具进行安装,其优点是简单同时也能够在需要的时候利用homebrew进行升级。(HomeBrew安装教程参见 https://brew.sh/)
**zhongxiao.yzx@MacBook-Pro:~/Workspace/ $brew info go** go: stable 1.10.3 (bottled), HEAD Open source programming language to build simple/reliable/efficient software https://golang.org Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb ==> Requirements Required: macOS >= 10.8 ✔ ==> Options --without-cgo Build without cgo (also disables race detector) --without-race Build without race detector --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 **zhongxiao.yzx@MacBook-Pro:~/Workspace $brew install go** Updating Homebrew... warning: unable to access '/Users/zhongxiao.yzx/.config/git/ignore': Permission denied warning: unable to access '/Users/zhongxiao.yzx/.config/git/attributes': Permission denied ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Renamed Formulae cdiff -> ydiff ==> Downloading https://homebrew.bintray.com/bottles/go-1.10.3.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring go-1.10.3.high_sierra.bottle.tar.gz ==> 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 ==> Summary
Go安装完成后,可以工作go env查看环境Go相关的环境变量
**zhongxiao.yzx@MacBook-Pro:~/Workspace $go env** GOARCH="amd64" GOBIN="" GOCACHE="/Users/zhongxiao.yzx/Library/Caches/go-build" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/zhongxiao.yzx/go" GORACE="" GOROOT="/usr/local/Cellar/go/1.10.3/libexec" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.10.3/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" CC="clang" CXX="clang++" CGO_ENABLED="1" 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/3l/nr_rmq_x0wd1nmg84kl9cfj00000gn/T/go-build353988632=/tmp/go-build -gno-record-gcc-switches -fno-common"
其中GOROOT,GOPATH是3个重要的环境变量
GOPATH: 日常开发的根目录,该目录在Go开发,编译,安装过程中起着重要的作用,例如 go get, go install等命令都会根据GOPATH来保存相关的插件,文件等。该目录不能和Go的安装目录一样,这个目录下面有三个子目录: src、bin、pkg
src 存放源代码(比如:.go .c .h .s等)
bin 编译后生成的可执行文件(为了方便,可以把此目录加入到$PATH变量中,如果有多个gopath,那么使用${GOPATH//://bin:}/bin)
GOBIN:是GOPATH下的bin目录,该环境变量影响到go install命令
**在未设置GOBIN情况下** zhongxiao.yzx@MacBook-Pro:~/Workspace/dev/golang/src$go install -x main.go go install: no install location for .go files listed on command line (GOBIN not set) **在设置了GOBIN环境变量的情况下** zhongxiao.yzx@MacBook-Pro:~/Workspace/dev/golang/src$go install -x main.go cd . /usr/local/Cellar/go/1.10.3/libexec/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=m26dg2-o1fDEe4lb1JZs/pcntLKDV1KQrpUm8zrkQ/FVueuXxUB9G0toBnk0P2/m26dg2-o1fDEe4lb1JZs -extld=clang $WORK/b001/_pkg_.a /usr/local/Cellar/go/1.10.3/libexec/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal mkdir -p /Users/zhongxiao.yzx/Workspace/dev/golang/bin/ mv $WORK/b001/exe/a.out /Users/zhongxiao.yzx/Workspace/dev/golang/bin/main rm -r $WORK/b001/
GOPATH在没有配置环境变量的情况下,Go开发环境采用默认的配置 GOROOT="/usr/local/Cellar/go/1.10.3/libexec" GOPATH="/Users/zhongxiao.yzx/go"
其中${HOME}是"/Users/zhongxiao.yzx/
zhongxiao.yzx@MacBook-Pro:~/Workspace $which go /usr/local/bin/go
那么为什么GOROOT="/usr/local/Cellar/go/1.10.3/libexec"呢?
zhongxiao.yzx@MacBook-Pro:~/Workspace $ls -la /usr/local/bin/go lrwxr-xr-x 1 zhongxiao.yzx admin 26 6 28 09:13 /usr/local/bin/go -> ../Cellar/go/1.10.3/bin/go zhongxiao.yzx@MacBook-Pro:~/Workspace $ls -la /usr/local/Cellar/go/1.10.3/bin/go lrwxr-xr-x 1 zhongxiao.yzx admin 17 6 7 07:50 /usr/local/Cellar/go/1.10.3/bin/go -> ../libexec/bin/go
可见/usr/local/bin/go实际上是对/usr/local/Cellar/go/1.10.3/ libexec/bin/go的引用
虽然go env以有默认的环境变量但是在终端中没有默认Go环境变量的设置,因此在.bash_profile中设置如下
#-------------------------------------------------- # go environment variable export GOROOT="/usr/local/Cellar/go/1.10.3/libexec" export GOPATH="${HOME}/Workspace/dev/golang" export GOPATH="${GOPATH}/bin" export PATH="${PATH}:${GOPATH}:${GOPATH}/bin"
至此我们可以构建如下的Go程序,并在终端环境采用go build, go run等命令编译运行Go程序 zhongxiao.yzx@MacBook-Pro:~/Workspace/dev/golang$tree -L 2 . ├── bin ├── pkg └── src
├── main.go
// main.go package main import ( "fmt" ) func main() { fmt.Println("hello world") } **zhongxiao.yzx@MacBook-Pro:~/Workspace/dev/golang/src$go -h** Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information bug start a bug report fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet report likely mistakes in packages Use "go help [command]" for more information about a command.
按照以上的步骤,我们已经建立了最原始的终端Go程序开发环境,但为了更加高效的进行大型Go程序的开发,需要搭建适合开发者的Go编程环境。 VSCode是流行的开源IDE,支持Go语言开发插件(VSCode安装参见 https://code.visualstudio.com)
点击VSCode Extensions图标,搜索Go插件,选择Go进行安装并重新加载Visual Studio Code。
VSCode插件安装依赖依赖GOPATH环境变量,虽然在第二部分已经配置了Go语言环境变量,但是在VSCode中GOPATH依然采用了默认值,因此需要在User Settings中设置go.gopath属性。
设置完Go环境变量,可以在Go工程中创建main.go文件,这时后VSCode会提示依赖缺失,提示安装
Installing 9 tools at /Users/zhongxiao.yzx/Workspace/dev/golang/bin gocode gopkgs go-outline go-symbols guru gorename godef goreturns golint Installing github.com/mdempsky/gocode SUCCEEDED Installing github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED Installing github.com/ramya-rao-a/go-outline SUCCEEDED Installing github.com/acroca/go-symbols SUCCEEDED Installing golang.org/x/tools/cmd/guru SUCCEEDED Installing golang.org/x/tools/cmd/gorename SUCCEEDED Installing github.com/rogpeppe/godef SUCCEEDED Installing github.com/sqs/goreturns SUCCEEDED Installing github.com/golang/lint/golint SUCCEEDED All tools successfully installed. You're ready to Go :).
当然也可以通过手动执行如下命令安装依赖插件
go get -u -v github.com/nsf/gocode go get -u -v github.com/rogpeppe/godef go get -u -v github.com/zmb3/gogetdoc 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 go get -u -v github.com/cweill/gotests/...
VSCode IDE要支持Go语言开发,执行以下命令安装Go编译调试插件。
go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv brew install go-delve/delve/delve go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
Go编译调试插件dlv需要证书的支持,按照以下步骤可以为dlv创建和修改证书 1.打开“钥匙串访问” 2.打开菜单->钥匙串访问->证书助理->创建证书 3.设置代码证书信息 4.单击“继续”,有限期(天数):365,可以自己修改,3650 5.一直继续,直到看到“指定用于该证书的位置”钥匙串,选择“系统”并单击“创建”按钮(必须选择“系统”类别,否则最后重新编译带代码签名dlv会失败) 6.重启Finder,再打开“钥匙串访问”,选择“系统”,就会看到创建好的“dlv”证书 7.右键"dlv"证书,选择“显示简介->信任->代码签名”修改为:始终信任 8.打开terminal,进入到之前安装好的$GOPATH/src目录下的dlv源码文件目录:github.com/derekparker/delve;输入如下命令就可以重新编译出一个带代码签名的dlv执行程序(CERT指定的证书名需要和创建名保持一致)
GO15VENDOREXPERIMENT=1 CERT=dlv make install
最后,退出重新打开VSCode就可以进行Go程序的开发和调试了
[1]. The Go Programming Language: https://golang.org/project/ [2]. Editor plugins and IDEs: https://golang.org/doc/editors.html [3]. vscode-go: https://github.com/Microsoft/vscode-go
GO语言 VSCode开发环境搭建 [Mac]介绍到这里,更多go学习请参考编程字典go教程 和问答部分,谢谢大家对编程字典的支持。
原文链接:https://blog.csdn.net/yzx_zjut/article/details/80838211?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163590318716780262548103%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163590318716780262548103&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-8-80838211.pc_v2_rank_blog_default&utm_term=go&spm=1018.2226.3001.4450