将 golang 项目推送到 heroku 时收到 "cannot execute binary file: Exec format error"。为什么会这样?

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

我最近一直在尝试使用部署以前的相同方法将一个简单的 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

我尝试 运行 go build -o bin/maingo build -o bin/main -v . 构建二进制文件,但是由于格式错误,heroku 无法构建它。我怎样才能解决这个问题?我运行在64位window平台上供参考。

此外,以防万一,这是我要部署的代码。

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))
}

如果您尝试在 bin/main 文件夹中构建文件,您需要这样做:

go build bin/main

如果您尝试在当前文件夹中构建,您需要这样做:

go build -o bin/main.exe

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

也可能是您正在尝试 运行 为具有不同体系结构的平台上的特定体系结构编译的可执行文件。尝试指定目标 os 和您希望 运行 构建的体系结构,如下所示。

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