操作系統(tǒng):CentOS6.9_x64
go語言版本: 1.8.3
安裝go
這里直接安裝二進(jìn)制,其它方式請自行搜索。
1、下載并安裝go
命令如下:
wgethttps://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz --no-check-certificatetarzxvf go1.8.3.linux-amd64.tar.gzmvgo /usr/local/
2、添加環(huán)境變量
vim /etc/profile
添加如下內(nèi)容:
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
使配置生效:
[root@localhost ~]# source /etc/profile
[root@localhost~]# go version
go version go1.8.3linux/amd64
[root@localhost~]#
使用go
這里以簡單的示例介紹下go語言的編譯、運(yùn)行,更深層次的內(nèi)容暫不討論。
文件名: test1.go 代碼:
package main
import"fmt"func main() {
fmt.Println("Email 1257834076@qq.com")
}
常規(guī)編譯運(yùn)行
1、通過go build 命令將go源碼編譯成二進(jìn)制文件;
2、執(zhí)行編譯好的二進(jìn)制文件即可。
示例如下:
[root@localhost src]# go build test1.go
[root@localhost src]#lstest1? test1.go
[root@localhost src]# ./test1
Email : Mike_Zhang@live.com
[root@localhost src]#
以腳本方式運(yùn)行
go語言可以直接通過go run直接運(yùn)行程序,可以借助此特性以腳本方式運(yùn)行g(shù)o程序。
方法1:
[root@localhost src]# go run test1.go
Email : Mike_Zhang@live.com
[root@localhost src]#
方法2:
在文件頭部加入如下代碼:
//usr/bin/env go run $0 "$@"; exit
然后通過chmod賦予可執(zhí)行權(quán)限即可。
示例如下:
[root@localhost src]#cattest1.go//usr/bin/env go run $0 "$@"; exitpackage main
import"fmt"func main() {fmt.Println("Email : Mike_Zhang@live.com")
}
[root@localhost src]#chmoda+x test1.go
[root@localhost src]# ./test1.go
Email : Mike_Zhang@live.com
[root@localhost src]#
好,就這些了,希望對你有幫助。