Go 环境部署、常用开发工具介绍

发布时间: 更新时间: 总字数:1683 阅读时间:4m 作者: IP上海 分享 网址

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

环境安装

Linux 源码包安装

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

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

wget https://golang.google.cn/dl/go1.21.0.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.21.0.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
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

生效:

source /etc/profile

查看go版本:

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

客户端安装

$ go install golang.org/dl/go1.21.4@latest
go: downloading golang.org/dl v0.0.0-20231107201224-8165dcd93058
$ go1.21.4 download
Downloaded   0.0% (   16384 / 67294613 bytes) ...
Downloaded  52.8% (35536672 / 67294613 bytes) ...
Downloaded 100.0% (67294613 / 67294613 bytes)
Unpacking /Users/xiexianbin/sdk/go1.21.4/go1.21.4.darwin-amd64.tar.gz ...
Success. You may now run 'go1.21.4'
$ go1.21.4 version
go version go1.21.4 darwin/amd64

Ubuntu snap 安装

snap install go --classic

apt

# /usr/lib/go-1.19/bin/go
apt install golang-1.19

# /usr/lib/go-1.20/bin/go
apt install golang-1.20
  • Golang-Backports PPA
sudo add-apt-repository ppa:longsleep/golang-backports -y
sudo apt update
sudo apt install golang

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

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>

gotip 安装最新版本

$ go install golang.org/dl/gotip@latest
$ gotip download

# 使用 gotip 替代 go
$ gotip version # 相当于执行 go version

多版本管理

开发/编辑工具

vscode

  • vscode + 插件
    • go插件(golang/vscode-go
    • Delve is a debugger for the Go,安装命令 go install github.com/go-delve/delve/cmd/dlv@latest
    • plugin go extension
    • 安装依赖 Ctrl/Command+Shift+P 输入 Go: Install/Update Tools 全部勾选后点击 OK

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/

代码风格

	anObject.
		MethodA().
		MethodB()

配置包

部分 golang.org 的包无法直接访问到,可以在 https://github.com/golang?q=mirror 中找到,手动clone到 $GOPATH/src/golang.org/x 目录下即可。

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

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以上的版本即可。

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数