Go Basics: The Relationship Between Paths, Filenames, and Package Names

Go Basics: The Relationship Between Paths, Filenames, and Package Names

Go package concept
1.Paths, Filenames.Put the same functionality into a directory, call it a package
2.Paths, Filenames.Packages can be referenced by other packages
3.Paths, Filenames.The main package is used to generate executable files, and each program has only one main package
4.Paths, Filenames.Packages improve code reusability

Paths, Filenames.Features of Go packages

There can only be one package in a folder.
•What follows import is actually the relative directory path starting with GOPATH , including the last paragraph. But since there can only be one package in a directory, importing a path is equivalent to importing the package under this path.
•Note that this refers to "directly included" go files. If there are subdirectories, then the parent of the subdirectory is exactly two packages.
oFor example, you have implemented a calculator package named calc, which is located in the calc directory; but you want to give others an example of use, so you can create an example subdirectory (calc/example/) under calc. In this subdirectory, there is a example.go (calc/example/ example.go ). At this point, example.go can be the main package, and there can also be a main function in it.

A package's files cannot be in multiple folders.
In the Golang documentation, the Language Specification page, under the Package clause, specifies
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.
That is to say, all files of a package must be located in the same directory
•If there are packages with the same name in multiple folders , they are actually unrelated packages.
•If a go file needs to use packages of the same name in different directories at the same time , you need to specify a package alias for each directory when importing these directories.
The package name can naturally be different from the folder name. After all, one is the import path and the other is the package name.
But it is not recommended to do this, it is easy to cause people who call this package to be unable to quickly know what the name of this package is.
As for why the directory name is not used as the package name, I think as everyone said, in order to avoid strange characters in the directory, and for the convenience of the caller
In the Golang documentation, the Language Specification page, under Import declarations, has this description
There are specific descriptions of PackageName and ImportPath in the official documentation of the Go language specification :
ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
ImportSpec = [ "." | PackageName ] ImportPath .
ImportPath = string_lit . _
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.
That is to say, when executing the import, if the package name is not manually defined, the package name is obtained from the package line in the source file of the import path, that is, the directory name and the package name are not directly related.

Paths, Filenames.in conclusion

1. The imported parameter of import is the path, not the package name.
2. Although it is customary to keep the package name and directory name consistent, this is not a mandatory requirement;
3. When referencing package members in code , use the package name instead of the directory name;
4. In the same directory, all source files must use the same package name (because the absolute path is used when importing, the package must have a unique path in the search path, but it does not need to be a unique name);
5. As for the file name, there are no restrictions (extension is .go);

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us