go 安裝配置(一)
基本
安裝gobrew install go
檢視go環境變數go ev
設定go環境變數vim .bash_profile
export GOPATH=和go環境變數顯示的一樣 export GOBIN=$GOPATH/bin export PATH=$PATH:$GOBIN
讓設定生效source .bash_profile
或者使用docker環境開發,我的環境:通過vagrant&virtualBox安裝centos安裝docker安裝golang docker.關於docker點選這裡
go mod
go mod支援golang1.11+,解決Go依賴更新的問題,並且讓專案原始碼可以在任意一個目錄放置,不再像以前那樣只能放在src.
mkdir testmod cd testmod
package testmod import "fmt" func Hi(name string) string { return fmt.Sprintf("Hi, %s", name) }
go mod init github.com/robteix/testmod
提交
git init git add * git commit -am "First commit" git push -u origin master
使用剛才建立的包
go get github.com/robteix/testmod
使用剛才建立的包或者
package main import ( "fmt" "github.com/robteix/testmod" ) func main() { fmt.Println(testmod.Hi("roberto")) }
在專案目錄根目錄執行
go mod init mod go build
更新包
go get -u go get -u=patch go get github.com/robteix/testmod@v1.0.1
go mod downloaddownload modules to local cache (下載依賴的module到本地cache)) editedit go.mod from tools or scripts (編輯go.mod檔案) graphprint module requirement graph (列印模組依賴圖)) initinitialize new module in current directory (再當前資料夾下初始化一個新的module, 建立go.mod檔案)) tidyadd missing and remove unused modules (增加丟失的module,去掉未用的module) vendormake vendored copy of dependencies (將依賴複製到vendor下) verifyverify dependencies have expected content (校驗依賴) whyexplain why packages or modules are needed (解釋為什麼需要依賴)