Understanding Go Modules & Building Go Programs

When we use “Go build” to build our executable file that could also be run on systems without Go installed, it will know where our program starts.

Before we can do that, we have to turn our project into a module. When we run go mod init name our project is going to be turned into a module. It adds a go.mod file into our project. This file tells Go that the folder in which this file is stored and all its sub-folders belong to a module of the name given.

When we then use go build we are getting an executable file which can be run without having Go installed.

When the name of our package wouldn’t be main, the go build command would not create any executable because it can not find our main entry point.

Last updated