一、下载go环境安装包
1.go官网地址
https://golang.google.cn/dl/
2.选择stable versions(稳定版本)
二、安装go环境
1.将软件包上传到linux服务器上,这里使用的是centos7.5环境
2.开始安装go语言环境安装包
[root@node3 ~]# tar xf go1.15.6.linux-amd64.tar.gz -C /usr/local/
3.添加环境变量
[root@node3 ~]# vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
#生效环境变量
[root@node3 ~]# source /etc/profile
#测试环境变量是否生效 ,显示go版本即为生效
[root@node3 ~]# go version
go version go1.15.6 linux/amd64
三、创建测试test用户编写程序
1.使用root用户创建测试用户test
[root@node3 ~]# useradd test
[root@node3 ~]# passwd test
Changing password for user test.
New password: #输入密码
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: #确认密码
passwd: all authentication tokens updated successfully.
2.登陆test用户,先查看PATH环境是否包含了go的环境变量
[test@node3 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/go/bin:/home/test/.local/bin:/home/test/bin
如果没有生效的话,在source /etc/profile
3.创建属于test的工作空间
[test@node3 ~]$ mkdir ~/go
[test@node3 ~]$ ls
go
4.添加工作目录的环境变量
[test@node3 ~]$ vim ~/.bash_profile
#添加到最后一行
export GOPATH=$HOME/go
[test@node3 ~]$ source .bash_profile
5.创建测试文件
[test@node3 ~]$ mkdir go/src
[test@node3 ~]$ cd go/src/
6.一般在src下是放项目的源码的,我们一般以项目名为单位创建相应的目录
[test@node3 first_go]$ vim test.go
package main
import "fmt"
func main(){ //这个"{" 的位置不能换行
fmt.Printf("hello go!\n") //Printf中的p必须大写
}
[test@node3 first_go]$ go build
#这里就编译好了一个和项目目录名称相同的可执行文件
[test@node3 first_go]$ ls
first_go test.go
#执行
[test@node3 first_go]$ ./first_go
hello go!
四、注释写法
1.当行注释: //
2.多行注释: /* */