One thing that makes it difficult for me to program in Go is that I must put modules (or I should call them packages?) that I want to import in my main module in separate directories, and even more, there must be an src
directory. Is there a way to import the file that resides in the same dir as the main module?
Current directory layout:
main.go
src/lib/lib.go
Now I can do import "lib"
What I would love to have:
main.go
lib.go
And still make the compiler happily find lib.go
.
This is the layout you want to have:
main.go
lib.go
And that is no problem. Go allows you to have such a file structure. And it is used by a lot of Go programms. But they both need to be defined as package main
. If you ask why? Because that is the language specification. The inventors of Go defined it like that.
The good thing here is, that you don't have to import anything, because the go compiler knows that these two files belong into the package main.