package main import ( "context" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) func main() { ctx := context.Background() client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27017")) if err != nil { panic(err) } defer func() { if err = client.Disconnect(ctx); err != nil { panic(err) } }() err = client.Ping(ctx, readpref.Primary()) if err != nil { panic(err) } }