Go語言學習一:環境搭建與第一個Demo
- 環境準備
- 資源下載
- 專案環境配置
- 第一個Demo
- 最后補充
- 感謝
環境準備
系統環境:Linux Ubuntu 16.04.6
Go語言資源包:go1.11.5.linux-amd64.tar.gz
資源下載
- cd到你想要保存的目錄
wget https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz- 下載完成后,將資源包解壓到
/usr/loacl目錄下
tar -C /usr/local -zxvf go1.11.5.linux-amd64.tar.gz - 添加
/usr/loacl/go/bin目錄到PATH變數中,添加到/etc/profile或$HOME/.profile都可以
vim /etc/profile
# 在最后一行添加
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
最后退出編輯進行一次source(管理員狀態)
source /etc/profile
- 執行
go version,看到go環境版本說明環境安裝配置成功
專案環境配置
- 創建作業空間,官方建議目錄
$HOME/gomkdir $HOME/go - 宣告作業變數
# 編輯 ~/.bash_profile 檔案 vim ~/.bash_profile # 在最后一行添加下面這句,$HOME/go 為你作業空間的路徑,你也可以換成你喜歡的路徑 export GOPATH=$HOME/go source ~/.bash_profile
第一個Demo
-
創建第一個Demo檔案,命名為
hello.go,并對hello.go進行編輯#創建并進入第一個工程目錄 mkdir -p $GOPATH/src/hello && cd $GOPATH/src/hello #創建hello.go檔案并編輯 vim hello.go -
寫入下面代碼
package main import "fmt" func main(){ fmt.Printf("Hello World!\n") } -
保存退出并運行代碼,即可看到運行結果
go run hello.go
最后補充
后來發現好像不用那么麻煩,可以使用sudo apt install golang-go直接進行安裝,并且不需要配置環境,大家可以嘗試一下
感謝
www.jianshu.com/p/c43ebab25484
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/189558.html
標籤:其他
