I have an issue with importing a local package in my go project. It seems that while I'm working locally I have to import with the full path: "github.com/username/myproject/mypackage". But when I push to heroku I get an error about not being able to find that package. If I change the import to "myproject/mypackage", it works on heroku, but no longer works locally.
The two solutions I've seen while researching seem like workarounds:
- Put my local packages into
vendor/
. My understanding is that the vendor directory is intended for third party libraries, so I'd rather not misuse that. - Move
myproject/
to be directly insrc/
. All the golang resources seem to stress that the file structure is very important and that I should have a github.com folder. I'm not clear on why the file structure is important, so this is the solution I'm going with right now, but I can't help but think I'm doing something wrong.
Has anyone else encountered this issue? I've included my file structure, .go files, and Godeps.json below for clarity.
My file structure:
$GOPATH
└───bin
└───pkg
└───src
└───github.com
└───username
└───myproject
│ main.go
└───Godeps
│ │ Godeps.json
└───mypackage
│ mypackage.go
mypackage.go
package mypackage
import (
"fmt"
)
func PrintHelloWorld() {
fmt.Println("Hello, world!")
}
main.go:
package main
import (
"fmt"
"github.com/username/myproject/mypackage" // import causing problems in heroku
"log"
"net/http"
"log"
)
func sayHelloWorld(w http.ResponseWriter, r *http.Request) {
mypackage.PrintHelloWorld()
fmt.Fprintf(w, "Hello world!")
}
func main() {
http.HandleFunc("/", sayHelloWorld)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Godeps.json:
{
"ImportPath": "myproject",
"GoVersion": "go1.10",
"GodepVersion": "v80",
"Deps": []
}
Error when pushing to heroku:
-----> Go app detected
-----> Checking Godeps/Godeps.json file.
-----> Using go1.10
!! Installing package '.' (default)
!!
-----> Running: go install -v -tags heroku .
main.go:6:2: cannot find package "github.com/username/myproject/mypackage" in any of:
/tmp/tmp.xY81RbKUze/.go/src/myproject/vendor/github.com/username/myproject/mypackage (vendor tree)
/app/tmp/cache/go1.10/go/src/github.com/username/myproject/mypackage (from $GOROOT)
/tmp/tmp.xY81RbKUze/.go/src/github.com/username/myproject/mypackage (from $GOPATH)
! Push rejected, failed to compile Go app.