Golang------Linux环境安装

下载golang编译包

wget https://golang.google.cn/dl/go1.16.2.linux-amd64.tar.gz

在这里插入图片描述

安装golang编译包

[root@localhost ~]#tar zxvf go1.16.2.linux-amd64.tar.gz -C /opt/
[root@localhost ~]#cd /opt/
[root@localhost opt]#ls
fisk.sh  go  rh
[root@localhost opt]#cd go/
[root@localhost go]#ls
api      CONTRIBUTING.md  favicon.ico  misc     README.md    src
AUTHORS  CONTRIBUTORS     lib          PATENTS  robots.txt   test
bin      doc              LICENSE      pkg      SECURITY.md  VERSION
[root@localhost go]#cd bin/
[root@localhost bin]#ls
go  gofmt				#/opt/go/bin/go文件是启动go编译器的文件

配置配置系统环境变量

[root@localhost bin]#pwd
/opt/go/bin
[root@localhost bin]#export PATH=/opt/go/bin:$PATH
[root@localhost bin]#echo $PATH
/opt/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

安装完成

[root@localhost bin]#cd
[root@localhost ~]#go
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
	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.

Additional help topics:

	buildconstraint build constraints
	buildmode       build modes
	c               calling between Go and C
	cache           build and test caching
	environment     environment variables
	filetype        file types
	go.mod          the go.mod file
	gopath          GOPATH environment variable
	gopath-get      legacy GOPATH go get
	goproxy         module proxy protocol
	importpath      import path syntax
	modules         modules, module versions, and more
	module-get      module-aware go get
	module-auth     module authentication using go.sum
	packages        package lists and patterns
	private         configuration for downloading non-public code
	testflag        testing flags
	testfunc        testing functions
	vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

[root@localhost ~]#go version
go version go1.16.2 linux/amd64

其他配置

创建一个任意目录

存放编写的所有项目代码,存放编译后的可执行文件

[root@localhost ~]#mkdir -p /opt/gggo/{bin,pkg,src}      #我创建在了/opt目录下
[root@localhost ~]#cd /opt/
[root@localhost opt]#ls
gggo  go  rh
[root@localhost opt]#cd gggo/
[root@localhost gggo]#ls
bin  pkg  src     #src存放编写的go代码和依赖

配置"持久化"环境变量

vim .bash_profile 
export PATH=/opt/go/bin:$PATH
export GOROOT=/opt/go		#配置go安装目录,源码目录,用于调用go相关源码
export GOPATH=/opt/gggo		#配置项目相关目录
export GOBIN=/opt/gggo/bin		#配置go编译文件目录,当使用go install 命令对代码进行编译是=时,可执行文件会生成在此目录
source /etc/profile

验证是否安装成功[root@localhost ~]#cd /opt/gggo/

[root@localhost gggo]#cd /opt/gggo/src/
[root@localhost src]#mkdir ceshi
[root@localhost src]#ls
ceshi
[root@localhost src]#cd ceshi/
[root@localhost ceshi]#touch app.go
[root@localhost ceshi]#ls
app.go
vim app.go
package main
import "fmt"
func main() {
    
    
	fmt.Println("今天是星期四")		#Println函数在屏幕上输出:今天是星期四
	}
[root@localhost gggo]#tree
.
├── bin				#存放编译后生成的可执行文件
├── pkg				#存放编译后生成的包文件
└── src				#存放所有编写的GO代码
    └── ceshi
        └── app.go

4 directories, 1 file

方法一

[root@localhost gggo]#cd src/ceshi/
[root@localhost ceshi]#go run app.go 
今天是星期四

方法二

[root@localhost ceshi]#go build app.go 		#编译代码并生成且只能生成可执行文件
[root@localhost ceshi]#ls
app  app.go
[root@localhost ceshi]#./app 
今天是星期四

方法三

[root@localhost ceshi]#go install app.go 		#编译后在/bin(可执行文件)或/pkg(包文件)目录生成文件
[root@localhost ceshi]#cd /opt/gggo/bin/
[root@localhost bin]#ls
app
[root@localhost bin]#./app 
今天是星期四

Goland下载网址:https://blog.jetbrains.com/go/
安装教程:http://c.biancheng.net/view/6124.html
active网址:http://lookdiv.com/

猜你喜欢

转载自blog.csdn.net/IvyXYW/article/details/114966232