I found the documents on “folder name”, “package name” and “module name” a bit confused to me. This is what I understood. Please correct me if I was wrong.
These plus workspace thing are much more complicated than module and package in Python.
Thanks!
folder name = package name = module name .
though the document does not say so, the three always match: folder name, package name, and module name (last part).
folder name : ./greetings
package name: package greetings
module name : module example.com/greetings
main package keep its name, the two of the rest still match.
import “name” and go mod init “name”, they are the same.
import "example.com/greetings"
go mod init "example.com/greetings"
sub-package in sub-dir needs one more level
import "example.com/hello/morestrings"
one folder = one package = one module .
but sub-package in sub-dir does not need to run go mod init command.
The code in the modules builds correctly. The extension golang.go v0.32.0 shows error message in VSCode. Seems even the extension can not understand the complex module, package and workspace things.
Error loading workspace: You are outside of a module and outside of $GOPATH/src. If you are using modules, please open your editor to a directory in your module.
Your editors workspace root does not contain a go.mod, so it doesn’t see a module.
To roughly answer your questions, at least as much as I understand them:
A module is the location where you find the sources and the module description, aka go.mod. The module is also a package and each package can contain subpackages, which are organised in a tree like manor. The foldernames are used for import, while the package name is used as qualifier in the code. Given a module example.com/foo/bar with a package quux would be accessible using quux in the importing file. package and folder name should only diverge if you have good reasons for it.
Not sure what you mean by “keep its name”
They are not. As there can (and usually are) be sub packages in the top level module.
Of cours, subfolders are subfolders, so you need to nest them. It is not any different on your local filesystem
One folder is one package (+ tests), but a module can have many folders and therefore subpackages. And go mod init creates a module, not a package, therefore you do not need to run it for subpackages. And you do not even need it for a module. In theory, you could create the bare go.mod on your own. Minimal content is module declaration followed by the go directive IIRC.
All source files in a folder use the same package name, right? For example, in a folder: hello, two files: a.go, b.go, should both use the name “hello” for the package directive:
package hello
A package consists of all the source files in a folder use the same name for package directory, is it correct?
Should the name in package directive be as same as the folder name?
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.
If, in a folder, there are different names occur in package declarations from source files, it will not build and show a message:
... found packages ...
And I did a test. Actually, folder, package and module name can be different. Module name is used in go mod init command, and import declaration.
If folder name is different from module name, it will need go mod edit - replace example.com/module_name=../folder_name.
If package name is different from module name, the exported name are still referenced in the same way: package_name.Hello(). While the import declaration look like import "module_name". It is just not easy to find out which module the package and exported name belong to.