How to Implement a Connection Pool in Go

Web
Configure a Go database connection pool by setting max open, max idle, and max lifetime limits on the DB object.

Use the database/sql package's DB.SetMaxOpenConns, SetMaxIdleConns, and SetMaxOpenConns methods to configure a connection pool after opening a database connection.

import "database/sql"

db, err := sql.Open("postgres", "user=postgres dbname=mydb sslmode=disable")
if err != nil {
    panic(err)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(10)
db.SetMaxIdleConns(10)

This creates a pool where up to 25 connections are active, 10 remain idle for reuse, and the pool automatically manages creation and closure of connections based on demand.