The problem

In the Go language, you are advised to arrange the software package directory structure in the format of organization name and project name. Generally, the project name file directory is divided into subdirectories based on functions, abstract conventions, and implementation. In Go language, different import paths of packages are considered as different packages. Therefore, if the “Function 1” package under the same software package project depends on the members in the “Function 2” package, the members must be exported members to be referenced by the “Function 1” package. But then, other projects or other organizations’ code can use this exported member. What if there are some members in the package that we want to share between the specified packages but don’t want to expose? The Go language internals feature allows us to achieve this goal.

internals

After version 1.4 of Go, the Internal packages feature was added to control the import of packages. That is, Internal packages can only be imported by specific packages.

Standard conventions for internal packages are as follows: If the exported path contains the keyword internal, only the internal parent directory and subpackages of the parent directory can be imported.

The sample

.
|-- resources
|   |-- internal
|   |   |-- cpu
|   |   |   `-- cup.go
|   |   `-- mem
|   |       `-- mem.go
|   |-- input
|   |   |-- input.go
|   `-- mainboard.go
|-- prototype
|   `-- professional.go 
|-- go.mod
|-- go.sum 
Copy the code

Resources /internal/ CPU and resources/internal/mem can only be imported from the resources package and its subpackage resources/input, not from the Prototype package. / / call prototype_resources /internal/ CPU; / / call prototype_resources /internal/ CPU; / / call prototype_resources /internal/ CPU; / / call prototype_resources /internal/ CPU;

use of internal package /resources/internal/cpu not allowed
Copy the code

conclusion

Internal/is a special directory name that the GO compiler recognizes when compiling a program, which will prevent another package from importing a package under the internal/ directory unless both packages have the same ancestor. Therefore, packages in the internal/ directory are referred to as internals.

To create an internal package for your project, simply place the package file in a directory named internal/. When the GO compiler sees an import of a package with internal/ in the import path, it verifies that the program files that imported the package are in the parent directory of the internal/ directory, or a subdirectory of the parent directory.

For example, a package whose import path is /a/b/c/internal/d/e/f can only be imported by code in/A /b/c or its subdirectories, but cannot be referenced by code in/A /b/e or its subdirectories.

Recommended reading

The use of the Go Pointer is restricted and unsafe.Pointer is the way to break through

That’s all for today’s article, please give me a thumbs up if you like my article, AND I will share what I have learned and seen and first-hand experience through technical articles every week. Thank you for your support. Wechat search concerns the public number “network management talk BI talk” every week to teach you an advanced knowledge.