Go基礎:路徑、檔名和包名的關係

哈哈哈hh發表於2021-09-22

工具與資源中心

幫助開發者更加高效的工作,提供圍繞開發者全生命週期的工具與資源

developer.aliyun.com/tool/?spm=a1z...

  1. 把相同的功能放到一個目錄,稱之為包
  2. 包可以被其他的包引用
  3. main包用來生成可執行檔案,每個程式只有一個main包
  4. 包可以提高程式碼的可複用性

一個資料夾下只能有一個package。

  • import後面的其實是GOPATH開始的相對目錄路徑,包括最後一段。但由於一個目錄下只能有一個package,所以import一個路徑就等於是import了這個路徑下的包。

  • 注意,這裡指的是“直接包含”的go檔案。如果有子目錄,那麼子目錄的父目錄是完全兩個包。

  • 比如你實現了一個計算器package,名叫calc,位於calc目錄下;但又想給別人一個使用範例,於是在calc下可以建個example子目錄(calc/example/),這個子目錄裡有個example.go(calc/example/example.go)。此時,example.go可以是main包,裡面還可以有個main函式。

一個package的檔案不能在多個資料夾下。

在 Golang 的文件中,Language Specification 頁面,Package clause 下,指明瞭

A set of files sharing the same PackageName form the implementation of a package. An implementation may require that all source files for a package inhabit the same directory.

也就是說,一個包所有的檔案,必須位於同一個目錄下

  • 如果多個資料夾下有重名的package,它們其實是彼此無關的package。
  • 如果一個go檔案需要同時使用不同目錄下的同名package,需要在import這些目錄時為每個目錄指定一個package的別名。

包名自然可以和資料夾名不一樣,畢竟一個是匯入路徑,一個是包名

但不建議這麼做,這樣容易造成呼叫這個包的人,無法快速知道這個包的名稱是什麼

至於為什麼不用目錄名作為包名,我想也正如大家所說,為了避免目錄中出現奇怪的字元,也為了呼叫者方便使用

在 Golang 的文件中, Language Specification 頁面,Import declarations 下,有這樣的說明

Go 語言規範官方文件中有對PackageNameImportPath的具體描述:

ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) . 
ImportSpec       = [ "." | PackageName ] ImportPath . 
ImportPath       = string_lit .

image.gif

The PackageName is used in qualified identifiers to access exported identifiers of the package within the importing source file. It is declared in the file block. If the PackageName is omitted, it defaults to the identifier specified in the package clause of the imported package. If an explicit period (.) appears instead of a name, all the package’s exported identifiers declared in that package’s package block will be declared in the importing source file’s file block and must be accessed without a qualifier.

也就是說,在執行匯入的時候,若不手動定義包名,則從匯入路徑的原始碼檔案中的 package 行獲取包名,也即目錄名和包名沒有直接的關係。

1、import 匯入的引數是路徑,而非包名。

2、儘管習慣將包名和目錄名保證一致,但這不是強制規定;

3、在程式碼中引用包成員時,使用包名而非目錄名;

4、同一目錄下,所有原始檔必須使用相同的包名稱(因為匯入時使用絕對路徑,所以在搜尋路徑下,包必須有唯一路徑,但無須是唯一名字);

5、至於檔名,更沒啥限制(副檔名為.go)

本文轉自:developer.aliyun.com/article/79177...

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章