一、下載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.多行註釋: /* */