Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Go module without main file

Tags:

go

go-modules

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

Shared
├── 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.

like image 836
JeyJ Avatar asked Mar 30 '26 23:03

JeyJ


1 Answers

If you only needed to build files in either the logging or db-utils directory, then you could executing the following from the root directory Shared:

go build <INSERT_MODULE_NAME_FROM_GO_MOD>/logging
go build <INSERT_MODULE_NAME_FROM_GO_MOD>/db-utils

I'm not certain if those commands will work if one of the files has a dependency on a file from the other directory.

Another command that will probably build the entire project is this:

go build ./...
like image 148
Toby Artisan Avatar answered Apr 02 '26 14:04

Toby Artisan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!