【Go語言入門系列】前面的文章:
- 【保姆級教程】手把手教你進行Go語言環境安裝及相關VSCode配置
- 【Go語言入門系列】(八)Go語言是不是面向物件語言?
- 【Go語言入門系列】(九)寫這些就是為了搞懂怎么用介面
1. GOPATH目錄結構
在【保姆級教程】手把手教你進行Go語言環境安裝及相關VSCode配置一文中已經配置過作業空間GOPATH的環境變數了,并在作業空間中新建了三個目錄src、pkg、bin了,那為什么要新建這三個目錄呢?這三個目錄又有什么作用呢?
首先,不管是什么系統或專案,目錄的存在肯定是為了使系統或專案的結構更加清晰、更加方便管理,在Go這里也不例外,
其次,其實只看名字,就能猜個大概:
src目錄:即source,用來存放源代碼,
pkg目錄:即package,用來存放編譯后的檔案,
bin目錄:即binary,用來存放編譯后生成的二進制的可執行檔案,
因為Go是用包來組織代碼的,而我們寫代碼的時候會參考別人的包,為了避免這些包名的重復,我們可以使用域名來作為包的前綴,對于個人來說,使用github賬號更加方便:
+- GOPATH
+- bin //存放編譯后的可執行檔案
+- pkg //存放編譯后的檔案
+- src //存放源代碼
+- github.com
| +- xingrenguanxue
| +- project1
| +- project2
| +- project3
+- gitee.com
+- golang.com
2. Go的命令工具
2.1. Go
打開命令列終端,輸入命令go就可以看到如下資訊:
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help <command>" for more information about a command.
Additional help topics:
buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions
Use "go help <topic>" for more information about that topic.
首先是一句簡明的介紹:
Go is a tool for managing Go source code.
Go命令是一個用來管理Go的源代碼的工具
然后告訴了我們Go命令工具的使用方式:
go <command> [arguments]
各種command也很貼心地羅列了出來,如果你不知道怎么使用,還可以使用命令go help <commnd>查看使用方式:
Use "go help <command>" for more information about a command.
比如,我想了解一些關于install命令的資訊,則使用命令go help install,即可查看,
還有一些topic,也可以使用go helo <topic>命令查看使用方式:
Use "go help <topic>" for more information about that topic.
所以如果以后對這些命令有疑惑,建議第一時間使用go help去查看官方是怎么回答的,
下面介紹一下go build、go run、go get、go install這四個命令,
2.2. 準備代碼
寫兩段代碼,用作測驗這三個命令的代碼,
打開作業空間,下面我們在src目錄下寫Go代碼
+- src
+- github.com
+- xingrenguanxue
+- main
| +- main.go
+- hello
+- hello.go
github.com\xingrenguanxue\main\main.go的代碼如下:
package main
import "fmt"
func main() {
fmt.Println("你好,世界!Hello,World!")
}
github.com\xingrenguanxue\hello\hello.go的代碼如下:
package hello
import "fmt"
func Hello() {
fmt.Println("這是hello包的Hello方法....")
}
使用【Ctrl+`】打開Terminal終端,下面的演示用終端進行操作(如上圖,現在終端在GOPATH\SRC目錄下),
2.3. go build
go build命令用來編譯包(一定要指明其匯入路徑)及其相關的依賴,
(一)go buildmain包
使用go build命令編譯包:
go build github.com\xingrenguanxue\main
可以看到會在main目錄下生成一個可執行檔案main.exe,進入該可執行檔案所在目錄并運行它:
cd github.com\xingrenguanxue\main
.\main
列印出了陳述句,
注意:現在我們是在編譯包main,如果你是這樣進行編譯的(未指明匯入路徑):
go build main
是會報錯的:
can't load package: package main: cannot find package "main" in any of:
C:\Go\src\main (from $GOROOT)
D:\Work\Program\go\src\main (from $GOPATH)
因為前面我們已經配置過環境變數了,go build的時候會從GOROOT\src或GOPATH\src下找包,而此例中main包在src\github.com\xingrenguanxue\目錄下,所以如果不帶上github.com\xingrenguanxue\,那么肯定會找不到的,
當然,你也可以進入該包所在目錄,直接使用go build命令而不指明包是對當前路徑的包進行編譯,和上面的方法本質上是一樣的:
cd github.com\xingrenguanxue\main
go build
(二)go build普通包
使用go build命令編譯包:
go build github.com\xingrenguanxue\hello
并沒有出現可執行檔案,因為main包中main函式——程式的入口,所以只有編譯main包才會出現可執行檔案,
(三)go build檔案
如果你想單獨編譯某個檔案,也是可以的,比如現在要編譯main.go檔案:
進入main包:
cd github.com\xingrenguanxue\main
編譯:
go build main.go
2.4. go run
我們使用go build命令時,需要先編譯成可執行檔案,然后在運行,而go run則將這兩步合起來做,并且并不會在目錄下生成可執行檔案,
進入main目錄下:
cd .\github.com\xingrenguanxue\main
使用go run進行編譯:
go run main.go
可以看到輸出了陳述句,
注意:go run命令編譯的物件必須包括main包下包含main函式的檔案,因為它還是要生成可執行檔案(雖然看不到)并幫我們運行,因此離不開main函式,
你可進入hello包下,去go run一下hello.go試一試,會報錯:
go run hello.go
go run: cannot run non-main package
2.5. go install
前面介紹過,GOPATH/bin目錄下存放的是我們編譯生成的可執行檔案,但是go build生成的可執行檔案是在源檔案目錄下,要將其存放在GOPATH/bin目錄下就得使用go install,所以該命令就相當于運行go build,然后將可執行檔案移動到GOPATH/bin目錄下,
go install github.com\xingrenguanxue\main
去GOPATH/bin目錄下就能找到main.exe檔案,
此時你再運行該可執行檔案,就不必再進入其所在目錄了,可在任意目錄下運行該可執行檔案,
之所以能這樣,是因為我們在配置環境的時候,已經把GOPATH\bin目錄加入環境變數Path中了,
2.6. go get
該命令用來從網上下載代碼到GOPATH中,并為我們編譯和安裝,
該命令會用到git,所以需要你先下載git,
go get -u github.com/gin-gonic/gin
比如我在我的的GitHub賬號xingrenguanxue中有一個名為hello-world的倉庫,目錄結構非常簡單:
+- hello-world
+- main.go
+- test
+- test.go
現在使用命令獲取該hello-world:
go get github.com/xingrenguanxue/hello-world
你可以在GOPATH下找到該hello-world,并且已經被編譯、安裝了,(GOPATH\bin下可以找到可執行檔案hello-world.exe),
如果遠程代碼庫的代碼更新了,可以使用以下命令更新本地代碼:
go get -u github.com/xingrenguanxue/hello-world
Go還提供了其他的命令工具,這里不再一一列出,其他命令可以使用go help具體查看,
作者簡介
【作者】:行小觀
【公眾號】:行人觀學
【簡介】:一個面向學習的賬號,用有趣的語言寫系列文章,包括Java、Go、資料結構和演算法、計算機基礎等相關文章,
本文章屬于系列文章「Go語言入門系列」,本系列從Go語言基礎開始介紹,適合從零開始的初學者,
歡迎關注,我們一起踏上編程的行程,
如有錯誤,還請指正,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/166739.html
標籤:Go
