Go App On Heroku With Local Packages

99封情书 提交于 2019-12-24 02:33:29

问题


I'm trying to put a Go app on Heroku using the Go Buildpack, which is fine when it's something basic, but as soon as I do a local package it does not compile. Here's an example setup:

Structure

+ship
  +foo  
    foo.go
  main.go

main.go

package main

import (
  "os"
  "fmt"
  "net/http"
  "ship/foo"
)

func main() {
  foo.Bar()
  port := os.Getenv("PORT")
  http.HandleFunc("/", root)
  http.ListenAndServe(":" + port, nil)
}

func root(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "Aloha, world!")
}

foo.go

package foo

func Bar() {}

Push

git push heroku master
Initializing repository, done.
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (20/20), 1.53 MiB | 586.00 KiB/s, done.
Total 20 (delta 2), reused 0 (delta 0)

-----> Fetching custom git buildpack... done
-----> Go app detected
-----> Installing go1.3.1... done
-----> Running: godep go install -tags heroku ./...
main.go:7:3: cannot find package "ship/foo" in any of:
    /app/tmp/cache/go1.3.1/go/src/pkg/ship/foo (from $GOROOT)
    /tmp/build_4b92e51c-3959-4ddb-8eff-90d72da70729/.heroku/g/src/_/Users/Daryl/Go/src/ship/Godeps/_workspace/src/ship/foo (from $GOPATH)
    /tmp/build_4b92e51c-3959-4ddb-8eff-90d72da70729/.heroku/g/src/ship/foo
godep: go exit status 1

 !     Push rejected, failed to compile Go app

Any idea what's going on here and how to go about it?


回答1:


Just a note for anyone coming across this issue in go 1.6. Godep has changed to use a vendor folder with Heroku instead, so you will need to reset your Godeps to use vendor as per the docs here:

https://github.com/tools/godep#go-15-vendor-experiment

Heroku also has upgrade info here:

https://devcenter.heroku.com/articles/go-support#migrating-from-go1-5-godep-workspace-to-go1-6-with-a-vendor-directory




回答2:


I have a solution that worked for me, though I don't like it, and hope its not the correct way to do it!

I'm using vendor. Both locally and TravisCI were able to build my app, but TravisCI could not deploy it to Heroku, as Heroku was having troubles finding local packages as well. What I ended up doing is fetching local packages with vendor:

govendor fetch +local

Once I committed again, TravisCI built and deployed to Heroku, and my app worked.

The reason I don't like this solution is that I now have duplicate code! My local subpackages can be found in /, as well as in vendor: /vendor/



来源:https://stackoverflow.com/questions/26076898/go-app-on-heroku-with-local-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!