登录
首页 >  Golang >  Go问答

遇到 "Exec 格式错误" 错误时如何解决将 golang 项目推送至 Heroku 的问题?

来源:stackoverflow

时间:2024-02-22 16:36:25 211浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《遇到 "Exec 格式错误" 错误时如何解决将 golang 项目推送至 Heroku 的问题?》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

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

2021-07-06t23:30:28.000000+00:00 app[api]: build started by user [email protected]
2021-07-06t23:30:40.271575+00:00 app[api]: release v7 created by user [email protected]
2021-07-06t23:30:40.271575+00:00 app[api]: deploy 0d19e552 by user [email protected]
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))
}

正确答案


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

env goos=linux goarch=arm64 go build -o bin/main

如果您尝试在 bin/main 文件夹中构建文件,则需要执行以下操作:

go build bin/main

如果您尝试在当前文件夹中构建,则需要执行以下操作:

go build -o bin/main.exe

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

理论要掌握,实操不能落!以上关于《遇到 "Exec 格式错误" 错误时如何解决将 golang 项目推送至 Heroku 的问题?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>