Received "cannot execute binary file: Exec format error" while pushing golang project to heroku. Why is this happening?

huangapple go评论119阅读模式
英文:

Received "cannot execute binary file: Exec format error" while pushing golang project to heroku. Why is this happening?

问题

我最近尝试使用与之前部署的应用相同的方法将一个简单的golang应用程序部署到Heroku,但在执行我的Procfile时遇到了错误。

我尝试运行go build -o bin/maingo build -o bin/main -v .来构建二进制文件,但由于格式错误,Heroku无法构建它。我在64位Windows平台上运行,请问我该如何解决这个问题?

此外,为了确保,以下是我尝试部署的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "github.com/go-chi/chi"
  9. "github.com/go-chi/render"
  10. "github.com/joho/godotenv"
  11. )
  12. type m map[string]interface{}
  13. func main() {
  14. godotenv.Load()
  15. router := chi.NewRouter()
  16. words := m{
  17. "black": 10,
  18. "red": 7,
  19. "pink": 0,
  20. "yellow": 5,
  21. "blue": 9,
  22. }
  23. router.Get("/all", func(res http.ResponseWriter, req *http.Request) {
  24. render.JSON(res, req, words)
  25. })
  26. router.Get("/add-color-rating/{color}/{rating}", func(res http.ResponseWriter, req *http.Request) {
  27. color := chi.URLParam(req, "color")
  28. rating, _ := strconv.Atoi(chi.URLParam(req, "rating"))
  29. words[color] = rating
  30. render.JSON(res, req, m{"added-rating": m{color: rating}})
  31. })
  32. fs := http.FileServer(http.Dir("./static"))
  33. router.Handle("/static/*", http.StripPrefix("/static", fs))
  34. fmt.Println("running on port:", os.Getenv("PORT"))
  35. log.Fatal(http.ListenAndServe(os.Getenv("PORT"), router))
  36. }

希望对你有帮助!

英文:

I have been recently attempting to deploy a simple golang app to heroku using the same method I used to deploy previous ones, and I'm running into error with my procfile execution.

  1. 2021-07-06T23:30:28.000000+00:00 app[api]: Build started by user user@yahoo.com
  2. 2021-07-06T23:30:40.271575+00:00 app[api]: Release v7 created by user user@yahoo.com
  3. 2021-07-06T23:30:40.271575+00:00 app[api]: Deploy 0d19e552 by user user@yahoo.com
  4. 2021-07-06T23:30:40.909377+00:00 heroku[web.1]: State changed from crashed to starting
  5. 2021-07-06T23:30:42.049118+00:00 heroku[web.1]: Starting process with command `bin/main`
  6. 2021-07-06T23:30:45.582197+00:00 app[web.1]: bash: bin/main: cannot execute binary file: Exec format error
  7. 2021-07-06T23:30:45.646220+00:00 heroku[web.1]: Process exited with status 126
  8. 2021-07-06T23:30:45.722720+00:00 heroku[web.1]: State changed from starting to crashed
  9. 2021-07-06T23:30:57.000000+00:00 app[api]: Build succeeded
  10. 2021-07-06T23:45:41.986316+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=testing-my-message-app.herokuapp.com request_id=fd3a8259-82dc-489f-b206-060ec0d8f8f6 fwd="67.81.209.179" dyno= connect= service= status=503 bytes= protocol=https
  11. 2021-07-06T23:45:42.522223+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=testing-my-message-app.herokuapp.com request_id=f3268609-cdcc-4c16-af24-f7dfa5568030 fwd="67.81.209.179" dyno= connect= service= status=503 bytes= protocol=https

I tried running both go build -o bin/main and go build -o bin/main -v .to build the binary file, but heroku is unable to build it for due to format error. How can I fix this? I'm running on a 64 bit window platform for reference.

Also, just in case, here is the code I am trying to deploy.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "github.com/go-chi/chi"
  9. "github.com/go-chi/render"
  10. "github.com/joho/godotenv"
  11. )
  12. type m map[string]interface{}
  13. func main(){
  14. godotenv.Load()
  15. router := chi.NewRouter()
  16. words := m{
  17. "black": 10,
  18. "red": 7,
  19. "pink": 0,
  20. "yellow": 5,
  21. "blue": 9,
  22. }
  23. router.Get("/all", func(res http.ResponseWriter, req *http.Request) {
  24. render.JSON(res, req, words)
  25. })
  26. router.Get("/add-color-rating/{color}/{rating}", func(res http.ResponseWriter, req *http.Request) {
  27. color := chi.URLParam(req, "color")
  28. rating, _ := strconv.Atoi(chi.URLParam(req, "rating"))
  29. words[color] = rating
  30. render.JSON(res, req, m{"added-rating": m{color: rating}})
  31. })
  32. fs := http.FileServer(http.Dir("./static"))
  33. router.Handle("/static/*", http.StripPrefix("/static", fs))
  34. fmt.Println("running on port:", os.Getenv("PORT"))
  35. log.Fatal(http.ListenAndServe(os.Getenv("PORT"), router))
  36. }

答案1

得分: 3

可能是因为您正在尝试在与不同架构的平台上运行针对特定架构编译的可执行文件。请尝试按照以下方式指定目标操作系统和架构,以便在构建时指定预期运行的目标操作系统和架构。

  1. env GOOS=linux GOARCH=arm64 go build -o bin/main
英文:

It could also be that you are trying to run an executable compiled for a specific architecture on a platform with a different architecture. Try specifying the the target os and architecture in which you expect to run the build as follows.

  1. env GOOS=linux GOARCH=arm64 go build -o bin/main

答案2

得分: 0

如果你想在bin/main文件夹中构建文件,你需要执行以下操作:

  1. go build bin/main

如果你想在当前文件夹中构建文件,你需要执行以下操作:

  1. go build -o bin/main.exe

https://golang.org/cmd/go#hdr-Compile_packages_and_dependencies

英文:

If you're trying to build files in the bin/main folder, you need to do this:

  1. go build bin/main

If you are trying to build in the current folder, you need to do this:

  1. go build -o bin/main.exe

<https://golang.org/cmd/go#hdr-Compile_packages_and_dependencies>

huangapple
  • 本文由 发表于 2021年7月7日 08:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/68278607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定