Having context.Context implementation issues with latest Go MongoDB driver

Pls find the code snippet and error below at end will discuss the issue

// go.mod

module hello

go 1.21.4

require go.mongodb.org/mongo-driver v1.13.1

require (

github.com/golang/snappy v0.0.1 // indirect

github.com/klauspost/compress v1.13.6 // indirect

github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect

github.com/xdg-go/pbkdf2 v1.0.0 // indirect

github.com/xdg-go/scram v1.1.2 // indirect

github.com/xdg-go/stringprep v1.0.4 // indirect

github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect

golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect

golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect

golang.org/x/text v0.7.0 // indirect

)

// main.go

package main

import (
	"context"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	// MongoDB connection string (replace with your actual connection string)
	connectionString := "mongodb://localhost:27017/test"

	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()

	// Connect to MongoDB
	client, err := mongo.Connect(ctx, options.Client().ApplyURI(connectionString))
	if err != nil {
		fmt.Println("Error connecting to MongoDB:", err)
		return
	}
	defer func() {
		if err := client.Disconnect(ctx); err != nil {
			fmt.Println("Error disconnecting from MongoDB:", err)
		}
	}()

	// Ping the MongoDB server to verify the connection
	err = client.Ping(ctx, nil)
	if err != nil {
		fmt.Println("Error pinging MongoDB:", err)
		return
	}

	fmt.Println("Connected to MongoDB!")
	// Now you can use the 'client' to interact with the MongoDB database
}

console output for above code is

# go.mongodb.org/mongo-driver/mongo
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:138:42: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.FindOneAndUpdate: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:170:37: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.InsertOne: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:280:35: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.DeleteOne: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:286:33: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.FindOne: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:293:33: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.FindOne: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:299:30: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.Find: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:309:42: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.FindOneAndUpdate: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\client_encryption.go:420:53: cannot use ctx (variable of type context.Context) as *context.Context value in argument to ce.keyVaultColl.BulkWrite: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\collection.go:195:9: cannot use context.Background() (value of type context.Context) as *context.Context value in assignment: context.Context does not implement *context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\collection.go:198:29: cannot use ctx (variable of type *context.Context) as context.Context value in argument to sessionFromContext: *context.Context does not implement context.Context (type *context.Context is pointer to interface, not interface)
..\..\..\go\pkg\mod\go.mongodb.org\mongo-driver@v1.13.1\mongo\collection.go:198:29: too many errors

Expected output : it should establish connection with my mongodb instance having version 4+ . but thowing above issue.Help me in resolving this issue.

Try to send &ctx (as pointer to ctx).

1 Like

That’s a weird compilation failure, none of those context.Context values should be pointers. It’s possible your Go module cache is corrupted somehow. I recommend cleaning your Go module cache and then try building your program again.

To clean the Go module cache:

go clean -modcache

Let me know if that fixes the compilation failures!