一個Golang小白的學習筆記,希望與大家共同學習,寫得不好的地方,請大家指正,多謝!~
雖然我們一般都在Windows作業系統上進行開發,但一般線上生產伺服器系統裝的都是Linux,因此熟悉Go語言在Linux上的安裝配置也是Go初學者必須掌握的技能。
Go語言官方網站為我們提供linux作業系統的二進位安裝包,可以非常簡單地安裝,除了使用二進位外,不同的Linux發行版也提供不同的第三方安裝工具,如Centos的yum和Ubuntu的apt-get。
安裝
1. 二進位包安裝(推薦)
根據自己的電腦是32位還是64位下載對應的安裝包,比如我下載的是64位的,
wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz //使用wget命令下載安裝包tar -zxvf -C /usr/local/go go1.10.3.linux-amd64.tar.gz //解壓到/usr/local/go目錄
2. Yum安裝
sudo yum install go
3. apt-get安裝
sudo apt-get install golang
配置環境變數
export GOROOT=/usr/lib/goexport GOARCH=386 //處理器加構,如果是64位的,應改為amd64export GOOS=linuxexport GOPATH=/home/gopathexport GOBIN=$GOPATH/binexport PATH=$GOPATH/bin:$PATH
1. GOROOT
GOROOT指向的是Go的安裝目錄。
2. GOPATH
工作區,按規範分為三個目錄:src,pkg,bin
一般GOPATH目錄如下
$GOPATH bin/ main.exe pkg/ windows_amd64 lib.a src/ main.go lib lib.go
在終端命令列輸入go命令,運行結果如下表示配置成功
$ goGo is a tool for managing Go source code.Usage: go command [arguments]The commands are: build compile packages and dependencies clean remove object files doc show documentation for package or symbol env print Go environment information bug start a bug report fix run go tool fix on packages fmt run gofmt on package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packagesUse "go help [command]" for more information about a command.Additional help topics: c calling between Go and C buildmode description of build modes filetype file types gopath GOPATH environment variable environment environment variables importpath import path syntax packages description of package lists testflag description of testing flags testfunc description of testing functionsUse "go help [topic]" for more information about that topic.
第一個Go語言程式
在GOPATH的src目錄建立main.go檔案,代碼如下
package mainimport "fmt"func main(){ fmt.Println("Hello World")}
運行
$ go run main.goHello World