gopl-zh.github.com/ch10/ch10-07-5.md

17 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 10.7.5. 內部包
在Go語音程序中包的封裝機製是一個重要的特性。沒有導出的標識符隻在同一個包內部可以訪問而導出的標識符則是面向全宇宙都是可見的。
有時候,一個中間的狀態可能也是有用的,對於一小部分信任的包是可見的,但併不是對所有調用者都可見。例如,當我們計劃將一個大的包拆分爲很多小的更容易維護的子包,但是我們併不想將內部的子包結構也完全暴露出去。同時,我們可能還希望在內部子包之間共享一些通用的處理包,或者我們隻是想實驗一個新包的還併不穩定的接口,暫時隻暴露給一些受限製的用戶使用。
![](../images/ch10-01.png)
爲了滿足這些需求Go語言的構建工具對包含internal名字的路徑段的包導入路徑做了特殊處理。這種包叫internal包一個internal包隻能被和internal目録有同一個父目録的包所導入。例如net/http/internal/chunked內部包隻能被net/http/httputil或net/http包導入但是不能被net/url包導入。不過net/url包卻可以導入net/http/httputil包。
```
net/http
net/http/internal/chunked
net/http/httputil
net/url
```