Go Security Guide 2025: Preventing Supply-Chain Attacks in CI/CD
Go Security Guide 2025: Preventing Supply-Chain Attacks in CI/CD...
Are you ready to master Golang for backend development? Whether you're just starting or want to level up your Go skills, this guide is designed to walk you through the entire process of building a fully functional REST API with MySQL integration and unit tests. By the end of this guide, you'll be able to confidently create scalable, production-ready APIs using Go.
In this blog, I'll break down the key steps to building a RESTful API that includes creating a simple database connection, implementing CRUD (Create, Read, Update, Delete) operations, and writing unit tests to ensure your code is reliable.
Before we get started, you need to set up Go and MySQL on your local machine or in a cloud environment.
If you haven’t installed Go yet, you can download it from the official website: Go Downloads.
Follow the installation instructions for your platform, and then verify the installation:
You can use a local MySQL instance or a cloud-based solution like AWS RDS or Google Cloud SQL. If you're running MySQL locally, install it via your package manager (Linux) or use tools like XAMPP or Docker to run MySQL on your machine.
Create a new database called go_rest_api for this project:
Now that you have your environment ready, let's initialize a new Go project.
Create a new directory for your project:
This will allow you to easily connect and interact with your MySQL database.
It's always a good practice to structure your project in a way that promotes maintainability and scalability. Here's a basic project structure:
In the database folder, create a file connection.go to handle the MySQL connection. Here's how you can do it:
In this example:
The DSN (Data Source Name) specifies the connection string with the format: username:password@protocol(address)/dbname.
The InitDB() function initializes the connection and returns the database instance.
Next, we need to define the model for our User resource. In the models folder, create user.go with the following content:
This User struct defines the fields we'll be using for the CRUD operations. The json tags are for JSON serialization when the data is returned in API responses.
In the handlers folder, create user_handlers.go to handle HTTP requests. Here’s how to implement a Create user function:
This handler function:
Decodes the JSON body into a User object.
Executes the SQL query to insert the user into the database.
Responds with a success message.
In routes/routes.go, set up the routes to map the HTTP requests to the respective handler functions:
In main.go, set up the server to start the API:
Unit tests are crucial to ensure your API works as expected. In the tests folder, create user_test.go to test the handlers.
By following this guide, you’ll be able to:
Connect Go with MySQL and perform basic CRUD operations.
Build production-ready REST APIs with Go.
Write unit tests to ensure your application works as expected.