Top Databases To Start Using With Golang In 2024
- With Code Example
- March 19, 2024
PostgreSQL, MySQL, SQLite, MongoDB, and Microsoft SQL Server with Go
Hey, I am going to discuss the leading databases you can start with golang. StackOverflow was surveyed in 2023 to choose these databases. So I will be using it as my point of reference and giving code examples on how to use those databases with golang this year 2024. So without wasting any more time let’s get started.
Flow diagram to connect to the database in Golang
graph TD; A[Start] --> B{Choose Database Type}; B -->|SQL Database| C[Import Database Driver]; B -->|NoSQL Database| D[Import Database Driver]; C --> D1[Set Database Connection Parameters]; D --> D1; D1 --> E[Open Database Connection]; E --> F[Execute SQL Queries / Operations]; F --> G[Close Database Connection]; G --> H[End];
Table of Contents
PostgreSQL
A relational database management system (RDBMS) is an open-source product called PostgreSQL, which supports SQL and other relational databases. It is also famous for its dependability, versatility, and backing of open standards in technology. PostgreSQL operates as a chief repository or storehouse of information for numerous web apps, mobile devices, geographical programs and analytical systems.
I have published a blog to start using Postgres with golang , you can check that out for a detailed overview.
Start using Postgres with Golang
- Install golang postgres package
go get github.com/lib/pq
- Code implementation for Golang Postgres
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
)
func main() {
// Establish a connection to the PostgreSQL database
db, err := sql.Open("postgres", "postgres://username:password@host:port/dbname?sslmode=disable")
if err != nil {
log.Fatal("Error connecting to the database: ", err)
}
defer db.Close()
// Check if the connection is successful
err = db.Ping()
if err != nil {
log.Fatal("Error pinging the database: ", err)
}
fmt.Println("Successfully connected to PostgreSQL!")
// Example query
rows, err := db.Query("SELECT * FROM your_table")
if err != nil {
log.Fatal("Error executing query: ", err)
}
defer rows.Close()
// Process query results
for rows.Next() {
// Process each row
}
}
MySQL
The free and open-source relational database management system (RDBMS) MySQL stores data in distinct tables rather than one single large database. It is an SQL-based RDBMS that can be run under various platforms such as Unix, Mac OS and Windows operating systems. MySQL is employed across many sectors and supports transactions, real-time analytics, and machine learning.
Start using MySQL with Golang
- Install Golang MySQL package
https://github.com/go-sql-driver/mysql
- Code implementation for Golang Mysql
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
fmt.Println("Go MySQL Tutorial")
// Open up our database connection.
// I've set up a database on my local machine using phpmyadmin.
// The database is called testDb
db, err := sql.Open("mysql", "root:password1@tcp(127.0.0.1:3306)/test")
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
// defer the close till after the main function has finished
// executing
defer db.Close()
// perform a db.Query insert
insert, err := db.Query("INSERT INTO test VALUES ( 2, 'TEST' )")
// if there is an error inserting, handle it
if err != nil {
panic(err.Error())
}
// be careful deferring Queries if you are using transactions
defer insert.Close()
}
SQLite
SQLite is a relational database management system (RDBMS) that is open source and serverless. It is a library embedded by software developers in their applications and acts as the most widely available database engine. SQLite is cross-platform and hence can be used on platforms such as macOS and Windows.
Start using SQLite with Golang
- Install Golang SQLite package
go get github.com/mattn/go-sqlite3
- Code implementation for Golang SQLite
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
func main() {
os.Remove("./foo.db")
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStmt := `
create table foo (id integer not null primary key, name text);
delete from foo;
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for i := 0; i < 10; i++ {
_, err = stmt.Exec(i, fmt.Sprintf("Transaction %d", i))
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
}
Click here for more examples
MongoDB
MongoDB is a non-relational database management system that is open-source, meaning its source code is available to the public. It uses documents and collections rather than tables to store data, hence it qualifies as a NoSQL or Not only SQL database. This means that you can store unstructured structured data in this DBMS due to its flexible data model with indexing support and replication using rich and intuitive APIs.
Start using MongoDB with Golang
- Install Golang MongoDB package
go get go.mongodb.org/mongo-driver/mongo
- Code implementation for Golang MongoDB
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Person struct {
Name string
Age int
City string
}
func main() {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// Access a MongoDB collection
collection := client.Database("testdb").Collection("people")
// Insert a document
_, err = collection.InsertOne(context.Background(), Person{"John Doe", 30, "New York"})
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted document into collection!")
// Find a document
var result Person
err = collection.FindOne(context.Background(), nil).Decode(&result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found document: %+v\n", result)
// Disconnect from MongoDB
err = client.Disconnect(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("Connection to MongoDB closed.")
}
Microsoft SQL Server
Microsoft SQL Server is made on Structured Query Language (SQL). It uses the programming language for database administrators (DBAs) to run and manage databases. Like other RDBMS software solutions, it is based on structured query language, a standard programming language used by database administrators (DBAs) and other IT professionals for managing databases and querying the data they contain. Transact-SQL(T-SQL) serves as the SQL server’s query language, which ensures instances or databases of SQL Servers are connected and communicated through various applications and tools from Microsoft alone.
Corporate IT environments can be supported by Microsoft SQL Server which is a relational database management system (RDBMS) that has a broad range concerning transaction processing, business intelligence (BI), and data analytics applications.
Start using Microsoft SQL Server with Golang
- Install Golang MsSqlDb package
go get github.com/denisenkom/go-mssqldb
- Code implementation for Golang MongoDB
package main
import (
"context"
"database/sql"
"fmt"
"log"
_ "github.com/denisenkom/go-mssqldb"
)
func main() {
// Connection parameters
server := "localhost"
port := 1433
user := "your_username"
password := "your_password"
database := "your_database"
// Connection string
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;",
server, user, password, port, database)
// Connect to SQL Server
conn, err := sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Error connecting to SQL Server:", err.Error())
}
defer conn.Close()
// Test the connection
err = conn.Ping()
if err != nil {
log.Fatal("Error pinging SQL Server:", err.Error())
}
fmt.Println("Connected to SQL Server!")
// Querying the database
rows, err := conn.QueryContext(context.Background(), "SELECT * FROM YourTable")
if err != nil {
log.Fatal("Error executing query:", err.Error())
}
defer rows.Close()
// Iterate through the result set
for rows.Next() {
var id int
var name string
var age int
if err := rows.Scan(&id, &name, &age); err != nil {
log.Fatal("Error scanning row:", err.Error())
}
fmt.Printf("ID: %d, Name: %s, Age: %d\n", id, name, age)
}
if err := rows.Err(); err != nil {
log.Fatal("Error iterating rows:", err.Error())
}
}
Conclusion
With this post, we took a look at some of the front-running databases that Go (Golang) can work with based on a StackOverflow survey done in 2023. These are; PostgreSQL, MySQL, SQLite, MongoDB and Microsoft SQL Server. Each has different features and capabilities useful for different software development goals.
We presented code samples to show how to connect and interact with these databases using Go. This empowers developers to easily integrate database functionality into their Go applications. Go provides powerful libraries and packages for effective database operations whether you are dealing with relational databases such as PostgreSQL, MySQL, and Microsoft SQL Server or non-relational ones like MongoDB and SQLite.
Armed with these insights plus the code examples provided, developers can start developing database-driven applications in Go by taking advantage of these leading databases’ power and flexibility.