Building Go module without main file


I have a small module that contains some shared code. The module looks like the following :

├── go.mod
├── go.sum
├── logging
│   └── logger.go
└── db-utils
    ├── db.go

If I’ll try to build the Shared module from inside the directory I’m getting an error that no go file available under this module.

bash-3.2$ go build .
no Go files in /Users/xxxx/go/src/Shared

Is there a way to build a Go module that only has packages inside and doesn’t have a main.go file? The motivation here is to use those packages in other go modules that can’t access the internet or private repo in order to retrieve the packages.


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.

If you want to distribute a non-public module for use with other modules, you have a few options:

  1. You can publish the module to a private repository — typically accessed with credentials stored in a .netrc file — and use the GOPRIVATE environment variable to tell the go command not to try to fetch it from the public proxy and checksum servers.

  2. You can provide a private GOPROXY server or directory containing the source code, and use the GOPROXY environment variable to instruct the go command to use that proxy.

  3. You can publish the source code as a directory tree and use a replace directive in the consumer’s go.mod file to slot it into the module graph.

Answered By – bcmills

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.