Home Archives Categories Tags

Go 环境部署和开发工具及常见问题

发布时间: 更新时间: 总字数:1452 阅读时间:3m 作者: 分享

本文介绍在 Windows、Linux 环境中安装部署 Go 环境;Go 常用命令;Go 语言开发工具; Go 部分包安装无法下载问题及解决方法

环境安装

Windows 安装

安装包下载地址:http://golang.org/dl/,下载 go1.7.4.windows-amd64.msi 安装包。

Windows 配置

添加GOROOT环境变量:

GOROOT=C:\Go\

在PATH中添加变量:

%GOROOT%\bin

添加GOPATH环境变量:

GOPATH=C:\GOPATH\

在PATH中添加变量:

%GOPATH%\bin

查看go版本:

C:\Users\Administrator>go
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
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         run go tool fix on packages
        fmt         run gofmt on 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         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

        c           calling between Go and C
        buildmode   description of build modes
        filetype    file types
        gopath      GOPATH environment variable
        environment environment variables
        importpath  import path syntax
        packages    description of package lists
        testflag    description of testing flags
        testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.


C:\Users\Administrator>

Linux 安装

  • 安装包下载地址:http://golang.org/dl/,下载源码包:go1.7rc3.linux-amd64.tar.gz

将下载的源码包解压至 /usr/local目录:

tar -C /usr/local -xzf go1.7rc3.linux-amd64.tar.gz

Linux 配置

/usr/local/go/bin 目录添加至PATH环境变量:

vim /etc/profile

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/gopath

生效:

source /etc/profile

查看go版本:

[root@xiexianbin_cn ~]# go version
go version go1.7rc3 linux/amd64

Mac 安装

  • 安装包下载地址:http://golang.org/dl/,下载源码包:go1.11.5.darwin-amd64.pkg,点击安装即可。

或:

brew install go

Mac 配置

查看go 的环境变量设置的命令

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xiexianbin/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xiexianbin/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/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/ql/nw5zyrg905z78mq4kwmq1lg00000gn/T/go-build830372641=/tmp/go-build -gno-record-gcc-switches -fno-common"

默认情况下GOPATHGOROOTGOBIN等均已经配置,用户可以在~/.bash_profile中重新定义,如下:

export GOPATH=/Users/xiexianbin/work/code/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:${GOPATH//://bin:}/bin

生效:

source ~/.bash_profile

开发/编辑工具

Eclipse 插件

eclipse的版本和安装 goeclipse 插件,参考如下说明:

Note for users in China

Note: if you are behind the Great Firewall of China, you are very likely to encounter problems installing GoClipse: blocked connections, timeouts, or slow downloads. This is because the update site is hosted in Github, which is blocked or has limited access. These alternative instructions should let you perform the installation:

Download an Eclipse installation which already contains CDT (such as the "Eclipse IDE for C/C++ Developers" package), so CDT doesn't have to be downloaded during installation.
Download the website from https://github.com/GoClipse/goclipse.github.io/archive/master.zip, unpack the archive and use the releases directory as a Local repository instead of the Update Site URL. Uncheck the option "Contact all updates sites during installation to find required software" so only the local repository is used.

参考地址:https://github.com/GoClipse/goclipse/blob/master/documentation/Installation.md

将go的开发源码放在GOPATH目录下的src目录下。

安装 eclipse 插件:

打开Window -> Preferences -> Go,配置 Tools 下的 gocodegurugodefgofmt

GoLand

下载地址:https://www.jetbrains.com/go/

配置包

guru

C:\GOPATH\src下创建目录:golang.org/x/tools/cmd/guru

guru下载:https://github.com/golang/tools,复制到golang.org/x/tools/运行:

go install golang.org/x/tools/cmd/guru

gocode

go get -u -v github.com/nsf/gocode
go install -u -v github.com/nsf/gocode

godef

go get -v github.com/rogpeppe/godef
go install -v github.com/rogpeppe/godef

其他

还要安装9fans.net/go/acme(目录需要自己在$GOPATH/src创建,源码在https://github.com/9fans/go下载)。。。

常用命令

编译

go 文件编译

set GOARCH=amd64
set GOOS=linux
go build main.go
GOOS=linux go build main.go

godoc

go 内置官方文档,可以在本地发布,运行:

godoc -http=:8080

访问:

http://127.0.0.1:8080/

go env

$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\GOPATH\
set GORACE=
set GOROOT=D:\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build050800186=/tmp/go-build -gno-record-gcc-switches

FAQ

guru 安装错误

安装guru错误:

C:\GOPATH\src>go install golang.org/x/tools/cmd/guru
golang.org\x\tools\cmd\guru\describe.go:11:2: cannot find package "go/constant"
in any of:
        C:\Go\src\go\constant (from $GOROOT)
        C:\GOPATH\src\go\constant (from $GOPATH)
golang.org\x\tools\cmd\guru\callees.go:11:2: cannot find package "go/types" in a
ny of:
        C:\Go\src\go\types (from $GOROOT)
        C:\GOPATH\src\go\types (from $GOPATH)
golang.org\x\tools\cmd\guru\describe.go:21:2: no buildable Go source files in C:
\GOPATH\src\golang.org\x\tools\go\types\typeutil

解决方法:Go版本问题,下载1.5以上的版本即可。

参考

最新评论
加载中...