Search code examples
gocompilation

What happens when you compile a sub-module of a golang project on linux/MacOS?


I'm sorry I might not have made the question clear in the title. Assuming I've got a project like this:

project_name/
--sub_module/
----somecode.go
--go.mod

in somecode.go

package main;
func main() {
    //do something
}

now if I run go build ./sub_module on Windows, a sub_module.exe file will be created in the project root

project_name/
--sub_module/
----somecode.go
--go.mod
--sub_module.exe

but from what I understand on Linux and MacOS, the sub module should be compiled into a file named sub_module(without the exe extension). However, in that case, it would have the exact same name as the sub_module directory. I'm kinda confused. Can someone help?


Solution

  • You're right, you'll get “build output "sub_module" already exists and is a directory”.

    As a workaround, you could do go build -o sub_module.exe ./sub_module (or use whatever name you like for the binary) or add a directory hierarchy, putting your “sub_module” command into a “cmd” folder, like most go code does. In the latter case, go build ./cmd/sub_module does what you expect on all platforms.