I'm trying to understand how to organize my golang project using go1.11 modules. I tried several options, but none of them worked.
I have a some code in the main package, ander application folder, and a local package that the main package uses.
$GOPATH
+ src
+ application/
+ main/
+ main.go
+ otherFileUnderMainPackage.go
+ aLocalPackage/
+ someCode.go
+ someCode_test.go
+ someMoreCode.go
+ someMoreCode_test.go
Files in the main package, imports ../aLocalPackage
. When I compile by go build main/*.go
it's working.
Then, I ran go mod init application:V.0.9.9
and got the go.mod file, but the build always fails. I always get error about not finding the local package: build application:V0.9.9/main: cannot find module for path _/.../src/application/aLocalPackage
. I also tried to place the local package right under src/, place it under main/ etc. but none of these method worked for me.
What is the way to use modules and local packages?
Thanks.
Relative import paths are not supported in module mode. You will need to update your import
statements to use a full (absolute) import path.
You should also choose a module name other than application
. Your module path should generally begin with a URL prefix that you control — either your own domain name, or a unique path such as github.com/$USER/$REPO
.