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
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个重要的环境变量
在未设置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开发环境采用默认的配置
其中${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程序
├── 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.
点击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创建和修改证书
GO15VENDOREXPERIMENT=1 CERT=dlv make install
最后,退出重新打开VSCode就可以进行Go程序的开发和调试了