查看centos系统版本
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
第一步:进入/tmp,使用wget下载go的源码包
[root@localhost ~]# cd /tmp
[root@localhost tmp]# wget https://golang.google.cn/dl/go1.18.3.linux-amd64.tar.gz
-bash:wget;未找到命令
# 如果想下载其他的版本,可以去golang的官方网站去下载。
[root@localhost tmp]# yum install wget -y # 下载wget
[root@localhost tmp]# wget https://golang.google.cn/dl/go1.18.3.linux-amd64.tar.gz
第二步:解压到当前目录下
[root@localhost tmp]# tar -xzvf go1.18.3.linux-amd64.tar.gz # 解压
[root@localhost tmp]# mv go /usr/local # 把go放在/usr/local
第三步:设置环境变量,在 ~/.bashrc 中添加。
[root@localhost tmp]# yum install vim -y # 下载vim编辑器
[root@localhost tmp]# vim ~/.bashrc
# 安装目录
export GOROOT=/usr/local/go
# 代码目录
export GOPATH=~/code
export PATH=$PATH:$GOPATH:$GOROOT/bin
# 添加以上代码,然后 source ~/.bashrc
[root@localhost tmp]# source ~/.bashrc
第四步:查看go的版本
[root@localhost ~]# go version
go version go1.18.3 linux/amd64
# 查看go的路径
[root@localhost tmp]# whereis go
go: /usr/local/go /usr/local/go/bin/go
IDE安装,Linux平台vim配置
Vim 是 Linux 下开发的最基本工具,可以配置一个 Vim IDE 。我们可以使用开源的安装工具,这里直接用该工具实现一键配置。
1.下载vim配置工具
[root@localhost tmp]# git clone https://github.com/lexkong/lexVim
下载慢的话可以去github上下载lexVim的压缩包,然后到Linux里解压
[root@localhost tmp]# yum install lrzsz -y
[root@localhost tmp]# yum install unzip -y
2.进入lexVim目录,下载go ide需要的二进制文件:
[root@localhost tmp]# cd/lexVim
[root@localhost lexVIm]# git clone https://github.com/lexkong/vim-go-ide-bin
3.启动安装脚本
[root@localhost lexVIm]# ./start_vim.sh
# 会发现权限不够,授权就可以了
[root@localhost lexVIm]# chmod +x start_vim.sh
[root@localhost lexVIm]# ./start_vim.sh
start_vim.sh : 行11:$'\r':未找到命令
start_vim.sh : 行11:未预期的符号 '$'\r' ' 附近有语法错误
[root@localhost lexVIm]# yum install dos2unix -y
[root@localhost lexVIm]# dos2unix start_vim.sh
安装这个工具可以解决,再次执行脚本:./start_vim.sh,会进入一个交换环境,依次输入:1 --> your name和 your E-mail
this vim config is success !
出现这个说明安装成功了
[root@localhost lexVIm]# vim a.go
vim IDE的常用功能
第一个go程序
[root@golang code]# cat example.go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
#直接运行
[root@golang code]# go run example.go
hello world
# 先编译再执行
[root@golang code]# go build example.go
[root@golang code]# ls
example example.go
[root@golang code]# ./example
hello world
go的常用命令
查看go的帮助文档, --help
[root@localhost tmp]# go --help
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
work workspace maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
问题(gd 不能跳转源码)
解决:配置安装源
1. 设置module管理模式
go env -w GO111MODULE=on
2.设置下载源
go env -w GOPROXY=https://goproxy.cn
3.下载guru
[root@golang code]# go get golang.org/x/tools/cmd/guru
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd@latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
[root@golang code]# go mod init mycode
go: creating new go.mod: module mycode
go: to add module requirements and sums:
go mod tidy
[root@golang code]# ls
example example.go go.mod pkg
[root@golang code]# go get golang.org/x/tools/cmd/guru
$GOPATH/go.mod exists but should not
[root@golang code]# rm -rf go.mod
[root@golang code]# mkdir test
[root@golang code]# cd test
[root@golang test]# ls
[root@golang test]# pwd
/root/code/test
[root@golang test]# go mod init mycode
go: creating new go.mod: module mycode
[root@golang test]# ls
go.mod
[root@golang test]# go get golang.org/x/tools/cmd/guru
go: downloading golang.org/x/tools v0.12.0
go: downloading golang.org/x/sys v0.11.0
go: downloading golang.org/x/mod v0.12.0
go: added golang.org/x/mod v0.12.0
go: added golang.org/x/sys v0.11.0
go: added golang.org/x/tools v0.12.0
[root@golang test]# go build golang.org/x/tools/cmd/guru
[root@golang test]# mv guru $(go env GOROOT)/bin
[root@golang test]#
# 做完以上操作就可以正常跳转源码了
测试工具安装
安装curl
[root@localhost tmp]# yum install curl -y