Database
58 articles
Connection pooling in Go
Go handles connection pooling automatically via http.Transport, allowing you to configure reuse limits and timeouts for better performance.
Connect to Redis
Use the official `go-redis/redis` library to connect to Redis by initializing a client with your server address and authentication details, then verify the connection with a simple `Ping` command.
Database migrations in Go
Go lacks a native migration tool, so use golang-migrate/migrate to execute SQL scripts against your database.
Fix: "sql: database is closed" in Go
Fix 'sql: database is closed' in Go by ensuring db.Close() is called only after all queries complete.
Fix: "sql: no rows in result set" in Go
Fix sql: no rows in result set by checking for sql.ErrNoRows after Scan or using Query with Next.
Fix: "too many connections" with Go Database Pools
Fix 'too many connections' in Go by setting MaxOpenConns, MaxIdleConns, and ConnMaxLifetime on your sql.DB instance.
GORM vs sqlx vs database/sql: Which to Use in Go
Use database/sql for raw performance, sqlx for easy struct mapping, or GORM for a full-featured ORM in Go.
How to Build a Repository Pattern in Go
Implement the Repository Pattern in Go by defining an interface for data operations and a concrete struct to separate business logic from data access.
How to Bulk Insert Rows in Go
Use database transactions or driver-specific copy commands to insert multiple rows efficiently in Go.
How to Choose Between SQL and NoSQL for Your Go App
Choose SQL for structured data and transactions, or NoSQL for flexible schemas and horizontal scaling in Go applications.
How to Connect to Cassandra from Go with gocql
Connect to Cassandra from Go using the gocql driver by creating a cluster and establishing a session.
How to Connect to ClickHouse from Go
Connect to ClickHouse from Go by installing the clickhouse-go driver and using sql.Open with the clickhouse:// DSN scheme.
How to Connect to DynamoDB from Go
Connect to DynamoDB from Go by initializing the AWS SDK v2 client and calling GetItem with your table name and primary key.
How to Connect to Elasticsearch from Go
Connect to Elasticsearch from Go using the official elastic/go-elasticsearch client library with a minimal configuration example.
How to Connect to MongoDB from Go
Connect to MongoDB from Go using the official driver package and the mongo.Connect function with your URI.
How to Connect to MySQL from Go
Connect to MySQL from Go using the go-sql-driver/mysql package and the standard database/sql library.
How to Connect to PostgreSQL from Go
Connect to PostgreSQL from Go using the pgx library's Connect function with a valid connection string.
How to Connect to Redis from Go
Connect to Redis from Go using the go-redis library by initializing a client with the server address and verifying the link with a Ping command.
How to Connect to SQLite from Go
Connect to SQLite in Go by importing a driver, registering it, and opening a connection with sql.Open.
How to Execute Queries with database/sql in Go
Execute SQL queries in Go by calling db.Query, iterating over rows, and scanning results into variables.
How to Handle Database Connection Timeouts in Go
Configure database connection timeouts in Go by setting DialTimeout and connection pool limits on your client, not via GODEBUG.
How to Handle Database Errors in Go
Handle Go database errors by checking return values and using errors.Is to identify specific issues like missing rows.
How to Handle NULL Values in Go SQL Queries
Handle SQL NULL values in Go by scanning into sql.Null* types and checking the Valid field before accessing the data.
How to Implement a Cache-Aside Pattern in Go
Implement Cache-Aside in Go by checking the cache first, fetching from the database on a miss, and storing the result before returning.
How to Implement Caching with Redis in Go
Use the go-redis library to connect to a Redis server and store data with Set and retrieve it with Get for fast access.
How to Implement the Repository Pattern in Go
Implement the Repository Pattern in Go by defining an interface for data operations and creating a struct that implements it to interact with your data source.
How to Implement the Unit of Work Pattern in Go
Implement the Unit of Work pattern in Go by creating an interface to track changes and a struct to manage a single database transaction for atomic commits.
How to Perform CRUD Operations with MongoDB in Go
Connect to MongoDB in Go and perform Create, Read, Update, and Delete operations using the official driver and BSON.
How to Prevent SQL Injection in Go
Prevent SQL injection in Go by using parameterized queries with placeholders instead of string concatenation.
How to Run Database Migrations in Go (golang-migrate, goose)
Install golang-migrate or goose and run the up command with your database URL to apply schema changes.
How to Scan Query Results into Structs in Go
Use the Scan method with pointers to struct fields to map database query results to Go structs.
How to Use Badger (Embedded Key-Value Store) in Go
Badger is a fast, embedded key-value store written in Go that you use by opening a database instance, performing transactions for writes, and iterating over keys for reads.
How to Use BoltDB / bbolt in Go
Use BoltDB in Go by opening a file, creating buckets in transactions, and storing key-value pairs with Put and Get methods.
How to Use Connection Pooling with database/sql
Configure connection pooling in Go by using sql.Open and setting MaxOpenConns, MaxIdleConns, and ConnMaxLifetime on the DB object.
How to Use Connection String DSN in Go
Go uses URL-style connection strings passed directly to sql.Open instead of system DSNs.
How to use database sql
Use the `database/sql` package as a generic interface to connect to any SQL database, then register a specific driver (like `pgx` for PostgreSQL or `mysql` for MySQL) to handle the actual connection details.
How to Use database/sql in Go: A Complete Guide
Connect to a database, run queries, and fetch results using the standard Go database/sql package with a single code example.
How to Use ent for Database Schema and Code Generation
Use `ent` by defining your data model in Go structs and then running the CLI to generate a type-safe, performant codebase that handles schema migrations and query building automatically.
How to Use etcd Client in Go
Use the official `go.etcd.io/etcd/client/v3` package to create a client, configure it with your cluster endpoints and timeout, and then perform operations like Put, Get, or Watch via the returned client object.
How to Use go-redis for Caching and Sessions in Go
Use the `go-redis` client to manage caching and sessions by treating Redis as a key-value store with configurable expiration times, leveraging its `Set` and `Get` methods for data retrieval and `Expire` for automatic cleanup.
How to use GORM
Initialize GORM by opening a connection, defining your data models as structs, and using built-in methods to create, read, update, and delete records.
How to Use GORM: A Complete Guide for Go
Initialize GORM by opening a connection and calling AutoMigrate on your struct to create the database table.
How to Use Memcached from Go
Connect to Memcached in Go using the gomemcache library to store and retrieve cached data efficiently.
How to use pgx
Use `pgx` by importing the `github.com/jackc/pgx/v5` module and initializing a connection pool with `pgxpool.New`, then execute queries using the `Query` or `Exec` methods on the pool or a transaction.
How to Use pgx for PostgreSQL in Go (Native Driver)
Connect to PostgreSQL in Go using pgx.Connect with a connection string and execute queries via QueryRow.
How to Use Prepared Statements in Go
Use db.Prepare() to create a reusable SQL statement with placeholders, then execute it with stmt.Query() or stmt.Exec() for safe, efficient database operations.
How to Use Redis Pub/Sub in Go
Connect to Redis in Go using the official client library to subscribe to channels and listen for real-time messages.
How to Use sqlc for Generating Type-Safe Database Code
sqlc generates type-safe Go database code from SQL queries by running `sqlc generate` with a configured `sqlc.yaml` file.
How to Use sqlc for Type-Safe SQL in Go
sqlc generates type-safe Go code from your SQL queries, eliminating the need for manual struct mapping and reducing runtime errors caused by mismatched column names.
How to use sqlx
Install sqlx via go get and use Connect, Select, and Get to map database rows directly to Go structs.
How to Use sqlx for Easier Database Access in Go
Use sqlx.Connect and sqlx.Select to map database rows directly to Go structs for cleaner code.
How to Use the Official MongoDB Driver for Go
Install and use the official go.mongodb.org/mongo-driver package to connect your Go application to MongoDB.
How to Use Transactions in Go with database/sql
Start a transaction with db.BeginTx, execute queries on the Tx object, and finalize with Commit or Rollback.
How to Write Raw SQL vs ORM in Go: Trade-Offs
Use Raw SQL for performance and control, and ORMs for speed and safety in Go database interactions.
Implement repository pattern in Go
Implement the repository pattern in Go by defining an interface for data operations and creating a struct that implements it to abstract database interactions.
MongoDB with Go
Connect to MongoDB in Go using the official driver and the ApplyURI method.
SQLite with Go
Connect to SQLite in Go using the database/sql package and a driver like go-sqlite3 to execute queries and manage data.
Transactions in Go
Use database/sql.BeginTx to start a transaction, execute queries, then Commit or Rollback to ensure data consistency.