本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《如何使用go和mongodb在heroku上部署应用程序而不会出现错误?》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
无法部署。出现错误:
cannot load go.mongodb.org/mongo-driver/mongo: open /tmp/build_aa982e7b99ad67f15e2c45be4077d6e9/vendor/go.mongodb.org/mongo-driver/mongo: no such file or directory
我尝试过导入方式:
1)“go.mongodb.org/mongo-driver/mongo”
在本地工作正常,但在部署过程中崩溃
2)“github.com/mongodb/mongo-go-driver/mongo”
不起作用
main.go:
package main
import (
"fmt"
"log"
"net/http"
"os"
"context"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
usernamekey = "xxx"
passwordkey = "xxx"
hostkey = "ds131905.mlab.com:31905"
databasekey = "heroku_zj4jbgpn"
)
func main() {
port := os.getenv("port")
if port == "" {
log.fatal("$port must be set")
}
router := gin.new()
router.use(gin.logger())
router.loadhtmlglob("templates/*.tmpl.html")
router.static("/static", "static")
router.get("/", func(c *gin.context) {
c.html(http.statusok, "index.tmpl.html", nil)
})
connecttomongodb()
router.run(":" + port)
}
func connecttomongodb() {
ctx := context.todo()
db, err := configdb(ctx)
if err != nil {
log.fatalf("database configuration failed: %v", err)
}
fmt.printf("found a single document: %+v\n", db)
}
func configdb(ctx context.context) (*mongo.database, error) {
uri := fmt.sprintf(`mongodb://%s:%s@%s/%s`,
usernamekey,
passwordkey,
hostkey,
databasekey,
)
client, err := mongo.newclient(options.client().applyuri(uri))
if err != nil {
return nil, fmt.errorf("couldn't connect to mongo: %v", err)
}
err = client.connect(ctx)
if err != nil {
return nil, fmt.errorf("todo: mongo client couldn't connect with background context: %v", err)
}
fmt.println("----->>>>> mongo client connected!!!")
tododb := client.database(databasekey)
return tododb, nil
}
部署日志:
-----> go app detected
-----> fetching stdlib.sh.v8... done
----->
detected go modules via go.mod
----->
detected module name: github.com/heroku/go-getting-started
----->
!! the go.mod file for this project does not specify a go version
!!
!! defaulting to go1.12.5
!!
!! for more details see: https://devcenter.heroku.com/articles/go-apps-with-modules#build-configuration
!!
-----> using go1.12.5
-----> determining packages to install
detected the following main packages to install:
github.com/heroku/go-getting-started
-----> running: go install -v -tags heroku -mod=vendor github.com/heroku/go-getting-started
build github.com/heroku/go-getting-started: cannot load go.mongodb.org/mongo-driver/mongo: open /tmp/build_aa982e7b99ad67f15e2c45be4077d6e9/vendor/go.mongodb.org/mongo-driver/mongo: no such file or directory
! push rejected, failed to compile go app.
! push failed
.bash_progile
export gopath=$home/go export path=$path:$gopath/bin
项目地点:
/users/denis/go/src/github.com/heroku/aqueous-ridge-14148/
mongo 驱动程序位置:
/Users/denis/go/src/go.mongodb.org/mongo-driver/
解决方案
我需要将我的应用程序从 $GOPATH/src 目录移动,并使用 go get go.mongodb.org/mongo-driver 安装 mongodb 驱动程序,如 this 教程中所示。
本篇关于《如何使用go和mongodb在heroku上部署应用程序而不会出现错误?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
