I have a Go project in JetBrains goland where all files are runnable yet independent of each other. But to make every runnnable, I need to make them as package main. And I have several "Vertex" defined elsewhere in other file and Goland complain about it. But it is still runnable, and that's purely complaint from Goland.
Question -
- Is there a better way to organized the files?
- If not, is there a way to turn off the complaint from Goland?
Working with multiple files that declare the
main()
function in the same directory is not recommended in general, mainly due to problems similar to yours.However, there are several ways to solve this.
You can use build constraints, also known as build tags, to separate the binaries at build time. When using them, the IDE will also need to be adjusted using the
Settings/Preferences | Build Tags & Vendoring
. And, depending how you build your application, you might also need to adjust the build command to add the corresponding tags to it.The other option, which I'd recommend in this case, is to move each
main()
defining file into a structure such as this:As an example of this layout, you can have a look at Delve, which uses a similar structure, but only has a single "command" in the
cmd
folder.Lastly, sometimes it's possible to remove the duplication and move it to a common file which holds the data type, but it's not always ideal and can make the build command more complex, since you need to specify all the files that should be included in the build process.
Edit:
And you can read more on how to organize your Go packages/applications here
These articles will explain how to organize your Go packages:
To understand more about the design philosophy for Go packages: https://www.goinggo.net/2017/02/design-philosophy-on-packaging.html