无法部署。出现错误:
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/
答案 0 :(得分:0)
我需要从$GOPATH/src
目录中移动我的应用程序,并像this教程中那样使用go get go.mongodb.org/mongo-driver
安装mongodb驱动程序。