Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Modules vs Package

Tags:

go

go-modules

just starting to learn about Go Modules. I have a question on importing local packages inside the same module. The example I am looking at is this repo:

https://github.com/Azure/azure-service-bus-go

The module is module github.com/Azure/azure-service-bus-go. There is a separate package inside that module, atom (but it's not a module itself). When files inside the main package import atom, they do it like this: import "github.com/Azure/azure-service-bus-go/atom" -- see queue_manager.go as an example.

What I do not quite understand -- how does GO know to look at the local atom package, as opposed to, say the one on Github? It seems confusing to me that something that is part of the module being modified is referenced by a remote/absolute URI. Is it guaranteed that if I modify the file on local disk and build I'm actually referencing the latest version as opposed to something already pushed?

As a toy exercise I tried to create a module with a non-existent Github URI and it did in fact appear that go mod tidy tried to look that up against Github, even though a local copy did in fact exist

like image 303
Yana K. Avatar asked May 21 '20 17:05

Yana K.


People also ask

What are Go modules?

Go modules commonly consist of one project or library and contain a collection of Go packages that are then released together. Go modules solve many problems with GOPATH , the original system, by allowing users to put their project code in their chosen directory and specify versions of dependencies for each module.

What is main module in Go?

The main module is the module containing the directory where the go command is invoked. Each package within a module is a collection of source files in the same directory that are compiled together. A package path is the module path joined with the subdirectory containing the package (relative to the module root).

Are Go modules compiled?

The go command stores downloaded modules in the module cache as source code, not compiled object files. go build rebuilds the object files on demand and stores them in a separate build cache.


2 Answers

The module directive in the go.mod file declares the import-path prefix for all of the packages within that module.

If you are just starting to learn about Go modules, the Create a Go module tutorial might be a good place to start.

like image 74
bcmills Avatar answered Oct 16 '22 13:10

bcmills


List item

  • A module is a collection of go packages.
  • A package is a directory of .go files. Using packages, you organize your code into reusable units.
  • We can add a module to go project or upgrade the module version.
like image 24
Hamed Naeemaei Avatar answered Oct 16 '22 12:10

Hamed Naeemaei