Linux下64 位 預編譯 的 go 1.6.2 安裝



go 安裝 - 1.6.2


OS: Linux, 64位, readhat 7.2

使用官網預編譯gz包安裝


1. 網址 https://golang.org/
目前版本 1.6.2


下載頁 https://golang.org/dl/


https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz




2. 解壓縮
go要求ROOT 目錄在 /usr/local 下,解壓命令:
[root@ip-172-30-0-110 home]# tar  -C /usr/local  -xf go1.6.2.linux-amd64.tar.gz 
[root@ip-172-30-0-110 home]# ls /usr/local/
bin  etc  games  go  include  lib  lib64  libexec  sbin  share  src




3. 修改/etc/profile 添加如下:
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH



source /etc/profile


測試
[root@ip-172-30-0-110 go]# which go
/usr/local/go/bin/go


[root@ip-172-30-0-110 go]# go version
go version go1.6.2 linux/amd64
[root@ip-172-30-0-110 go]# 




4. 運行helloworld程序
hello.go內容


package main
import  "fmt"


func main() {
   fmt.Printf("Hello world!\n");
}



編譯運行
[root@ip-172-30-0-110 go]# vi  hello.go
[root@ip-172-30-0-110 go]# go run  hello.go 
Hello world!
[root@ip-172-30-0-110 go]# 




5. 編譯hello項目
在當前目錄下, 創建目錄
[root@ip-172-30-0-110 gopath]# mkdir bin pkg src
[root@ip-172-30-0-110 gopath]# ls
bin  pkg  src
按照如下方式放置文件
[root@ip-172-30-0-110 gopath]# tree .
.
├── bin
├── pkg
└── src
    └── hello
        └── hello.go


在當前目錄下,編譯hello項目
[root@ip-172-30-0-110 gopath]# export GOPATH=`pwd`; go install hello
[root@ip-172-30-0-110 gopath]# tree .
.
├── bin
│   └── hello
├── pkg
└── src
    └── hello
        └── hello.go


4 directories, 2 files
[root@ip-172-30-0-110 gopath]# 


運行可執行文件
[root@ip-172-30-0-110 gopath]# bin/hello 
Hello world!
[root@ip-172-30-0-110 gopath]# 


說明:
a) go在兩個地方找程序包
$GOROOT 下的src目錄, 以及 $GOPATH 下的src 目錄


報錯示例如下:
[root@ip-172-30-0-110 gopath]# go install hello.go 
go install: no install location for .go files listed on command line (GOBIN not set)


[root@ip-172-30-0-110 gopath]# go install hello
can't load package: package hello: cannot find package "hello" in any of:
/usr/local/go/src/hello (from $GOROOT)
($GOPATH not set)



b)項目的名字可以更換, 
如將 src/hello, 更改爲test, 那麼項目的名字,也就變成了 test, 如使用go build 命令,變如下:
[root@ip-172-30-0-110 gopath]# go build test
[root@ip-172-30-0-110 gopath]# ls -alht
total 2.2M
drwxr-xr-x  5 root root   47 Jun  6 03:01 .
-rwxr-xr-x  1 root root 2.2M Jun  6 03:01 test
drwxr-xr-x  2 root root   17 Jun  6 03:00 bin
drwxr-xr-x  3 root root   17 Jun  6 02:54 src
drwxr-xr-x  2 root root    6 Jun  6 02:43 pkg
drwxr-xr-x. 7 root root 4.0K Jun  6 02:41 ..
[root@ip-172-30-0-110 gopath]# 
目錄結構
.
├── bin
├── pkg
├── src
│   └── test
│       └── hello.go
└── test





6. mercurial工具
yum install mercurial
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章