# Go driver + Atlas
```go
package main
import (
"context"
"os"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func connectDB() (*mongo.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx,
options.Client().
ApplyURI(os.Getenv("MONGODB_URI")).
SetMaxPoolSize(20).
SetServerSelectionTimeout(5*time.Second),
)
if err != nil {
return nil, err
}
if err := client.Ping(ctx, nil); err != nil {
return nil, err
}
return client, nil
}
```
## Rules
- Always pass a context with a timeout to `Connect` and `Ping`. Without it a bad network path hangs forever.
- `ApplyURI` parses the Atlas SRV string including credentials and options. Do not hand-assemble hosts.
- Share the returned `*mongo.Client` across goroutines; it is safe for concurrent use and owns the pool.
- On shutdown call `client.Disconnect(ctx)` so pooled connections close cleanly instead of lingering until the server times them out.
- `SetMaxPoolSize` caps total connections. Size it from expected concurrent operations, not from wishful thinking.
## Verify
`Ping` succeeding from the deploy network proves URI, credentials, and IP access list in one shot.