简体   繁体   English

将 golang 项目推送到 heroku 时收到“无法执行二进制文件:Exec 格式错误”。 为什么会这样?

[英]Received “cannot execute binary file: Exec format error” while pushing golang project to heroku. Why is this happening?

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.我最近一直在尝试使用与部署以前的应用程序相同的方法将一个简单的 golang 应用程序部署到 heroku,但我在执行 procfile 时遇到了错误。

2021-07-06T23:30:28.000000+00:00 app[api]: Build started by user user@yahoo.com
2021-07-06T23:30:40.271575+00:00 app[api]: Release v7 created by user user@yahoo.com
2021-07-06T23:30:40.271575+00:00 app[api]: Deploy 0d19e552 by user user@yahoo.com
2021-07-06T23:30:40.909377+00:00 heroku[web.1]: State changed from crashed to starting
2021-07-06T23:30:42.049118+00:00 heroku[web.1]: Starting process with command `bin/main`
2021-07-06T23:30:45.582197+00:00 app[web.1]: bash: bin/main: cannot execute binary file: Exec format error
2021-07-06T23:30:45.646220+00:00 heroku[web.1]: Process exited with status 126
2021-07-06T23:30:45.722720+00:00 heroku[web.1]: State changed from starting to crashed
2021-07-06T23:30:57.000000+00:00 app[api]: Build succeeded

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      
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 .我尝试同时运行go build -o bin/maingo build -o bin/main -v . to build the binary file, but heroku is unable to build it for due to format error.构建二进制文件,但由于格式错误,heroku 无法构建它。 How can I fix this?我怎样才能解决这个问题? I'm running on a 64 bit window platform for reference.我在 64 位窗口平台上运行以供参考。

Also, just in case, here is the code I am trying to deploy.另外,为了以防万一,这是我要部署的代码。

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "strconv"

    "github.com/go-chi/chi"
    "github.com/go-chi/render"
    "github.com/joho/godotenv"
)

type m map[string]interface{}

func main(){
    godotenv.Load()

    router := chi.NewRouter()
    words := m{
        "black": 10,
        "red": 7,
        "pink": 0,
        "yellow": 5,
        "blue": 9,
    }

    router.Get("/all", func(res http.ResponseWriter, req *http.Request) {
        render.JSON(res, req, words)
    })

    router.Get("/add-color-rating/{color}/{rating}", func(res http.ResponseWriter, req *http.Request) {
        color  := chi.URLParam(req, "color")
        rating, _ := strconv.Atoi(chi.URLParam(req, "rating"))

        words[color] = rating
        render.JSON(res, req, m{"added-rating": m{color: rating}})
    })
    
    fs := http.FileServer(http.Dir("./static"))
    router.Handle("/static/*", http.StripPrefix("/static", fs))

    fmt.Println("running on port:", os.Getenv("PORT"))
    log.Fatal(http.ListenAndServe(os.Getenv("PORT"), router))
}

If youre trying to build files in the bin/main folder, you need to do this:如果您尝试在bin/main文件夹中构建文件,则需要执行以下操作:

go build bin/main

If you are trying to build in the current folder, you need to do this:如果您尝试在当前文件夹中构建,则需要执行以下操作:

go build -o bin/main.exe

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

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.尝试指定您希望在其中运行构建的目标操作系统和体系结构,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 golang 无法执行二进制文件:执行格式错误 - golang cannot execute binary file: Exec format error 部署到 Heroku 错误无法执行二进制文件执行格式错误 - Deploy to Heroku Error cannot execute binary file Exec format error 使用Golang Exec执行Tmux - Execute Tmux with Golang Exec 当我尝试使用 exec 在 golang 中的 cmd 上执行以下代码时,执行命令给出错误“没有这样的文件或目录”。 有人可以帮我弄这个吗? - Exec command giving error "No such file or directory" when I try to execute the below code on cmd in golang with exec. Can someone help me with this? Heroku上的Golang二进制文件 - Golang binary on Heroku 为什么我的 golang 程序有这个错误。 exec: "lib/synonyms": 文件不存在 - Why is my golang program having this error. exec: "lib/synonyms": file does not exist 运行AWS Golang Lambda时出现exec格式错误 - exec format error when running AWS Golang Lambda 尝试在 golang 中执行 python 可执行文件时执行格式错误 - exec format error when trying to excute a python excutable in golang 从golang的exec.Command调用ghostscrip时出错 - getting error while call ghostscrip from exec.Command of golang 在 golang 示例中执行 kubectl get po -o yaml 时出错 - Error while exec kubectl get po -o yaml in golang example
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM