如何解决部署到Heroku错误无法执行二进制文件Exec格式错误
我是Golang的新手,正在尝试将简单的rest api部署到Heroku。
在我构建代码并在本地运行heroku之后,该代码在本地工作。但是,当我将其推送到Heroku时,该代码不起作用。在浏览器中,我看到heroku的应用程序错误
我使用heroku日志--tail跟踪以下错误日志
错误代码为“无法执行二进制文件:执行格式错误”。我做了一次Google搜索,从我的阅读中得知编译版本不兼容。如果是这样,我该怎么做才能使正确的二进制文件在heroku上运行?谢谢
2020-08-14T11:48:37.863717+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T11:48:37.909304+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T11:48:37.946300+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-14T11:48:59.000000+00:00 app[api]: Build succeeded
2020-08-14T11:52:47.670135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=go-hendry.herokuapp.com request_id=06309354-6516-4520-8800-5fa12b8b421b fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T11:52:49.872040+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=go-hendry.herokuapp.com request_id=b8f0a6a6-06cc-4bef-a346-21b91ce024d2 fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T12:03:31.503635+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-14T12:03:32.679883+00:00 heroku[web.1]: Starting process with command `bin/golang-gin-poc`
2020-08-14T12:03:35.223469+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T12:03:35.263368+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T12:03:35.306058+00:00 heroku[web.1]: State changed from starting to crashed
下面是我的server.go代码,以供参考
package main
import (
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/myhendry/goapi/controller"
"github.com/myhendry/goapi/service"
)
var (
videoService service.VideoService = service.New()
videoController controller.VideoController = controller.New(videoService)
)
func setupLogOutput() {
f,_ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f,os.Stdout)
}
func main() {
setupLogOutput()
server := gin.New()
server.Use(gin.Logger())
apiRoutes := server.Group("/api")
{
apiRoutes.GET("/videos",func(ctx *gin.Context) {
ctx.JSON(200,videoController.FindAll())
})
apiRoutes.POST("/videos",func(ctx *gin.Context) {
err := videoController.Save(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest,gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusOK,gin.H{"message": "Video Input is Valid!!"})
}
})
}
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
server.Run(":" + port)
}
解决方法
在终端上的本地开发计算机Mac OS上编译
env GOOS=linux GOARCH=amd64 go build -o bin/golang-gin-poc -v .
然后推送到Heroku
我仍在寻找在Heroku上进行后期构建的解决方案
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。