Starting a Go project with the right code organization means no time lost in case you wanted to publish your modules in a public repository.
Even not considering publishing, keeping the source code properly organized will help you and your teammates to quickly find those needed resources.
The next schema shows the officially recommended way of organizing Go code.
In a nutshell:
- Every Go source code file (extension
.go
) must define the package to which its contents (types, functions, ...) belongs to - All the source files in the same subdirectory belongs to the same package
- A module contains a set of packages, stored in subdirectories of the modules' root directoy.
- A module defines its packages in a file named
go.mod
- Optionally a module can be published in a repository (as for instance github).
- Each package can be imported to a Go project as
<repository>/<module>/package
- The whole module becomes publicly available as
<repository>/<module>
Comentarios
Publicar un comentario