![]() |
VOOZH | about |
Go, often referred to as Golang, is a programming language that has gained immense popularity due to its simplicity, speed, and strong support for concurrent programming. It was developed by Google to tackle large-scale system programming challenges, and one of its key advantages is its ability to efficiently manage multiple processes simultaneously. This makes Go particularly well-suited for backend development, where handling a large number of database queries concurrently is a common task.
Table of Content
When working with databases, Go’s standard library provides the database/sql package, which offers a unified interface for interacting with various SQL databases, including MySQL. The package is efficient, minimalistic, and easy to use, making it a great choice for developers who want to integrate database functionality into their Go applications. Additionally, Go’s support for concurrency through goroutines ensures that database operations can be handled efficiently even under heavy load, making it a perfect fit for building scalable web applications and microservices.
In this article, we will explore how to use Go to interact with a MySQL database, focusing on executing core SQL operations: SELECT, INSERT, UPDATE, and DELETE. These operations form the backbone of most database-driven applications, and understanding how to perform them in Go is essential for any developer looking to build robust backend systems.
In order to execute MySQL Queries in Go.
Next, We're going to be start our main function
here db as a tuple object and err as error is returned when sql.Open query is used. Connection type, Username, Password, Local host address along with db name is specified to make a connection with the database.
defer the close till after the main function has finished
To retrieve a single row from the database, we use QueryRow combined with the Scan method:
In this case, we are fetching the name of the user with the id of 1. We use the Scan method to store the result in the name variable.
To fetch multiple rows, we use Query and iterate over the result set:
Here, we retrieve all id and name pairs from the users table and print them out.
Inserting data into MySQL is straightforward with Go’s Exec method. Here's an example of how to add a new user to the users table:
The Exec method is used to execute non-SELECT queries like INSERT, and LastInsertId returns the ID of the inserted row.
Updating data is similar to inserting. You’ll again use the Exec method, but this time to modify an existing record:
In this example, the UPDATE query changes the name of the user with id = 1 to "Bob". The RowsAffected method returns how many rows were updated.
To remove data from the database, you can use a DELETE query. Here's how you would delete a user from the users table:
This code deletes the user with id = 1 and prints how many rows were removed.
Handling errors is crucial when working with databases. Every database operation, whether it’s connecting, querying, or scanning, can fail. Therefore, you must always check for errors and handle them gracefully:
Each query should be followed by an error check to ensure proper error handling and debugging. Failing to do so can lead to program crashes or unexpected behavior.
In this article, we covered how to execute different types of MySQL queries (SELECT, INSERT, UPDATE, DELETE) using Go. With Go’s database/sql package and MySQL driver, you can efficiently interact with MySQL databases and handle various query types with ease.