
Build a Complete REST API in Go – From Zero to Production!
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.
Step 1: Setting Up Your Golang Environment
Before we get started, you need to set up Go and MySQL on your local machine or in a cloud environment.
1.1 Installing Golang
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:
1.2 Installing MySQL
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:
Step 2: Initializing the Go 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.
Step 3: Structuring the Project
It's always a good practice to structure your project in a way that promotes maintainability and scalability. Here's a basic project structure:
Step 4: Connecting Go with MySQL
4.1 Create a Database Connection
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.
Step 5: Defining Models
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.
Step 6: Implementing CRUD Operations
6.1 Create User
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
Userobject. -
Executes the SQL query to insert the user into the database.
-
Responds with a success message.
6.2 Read User (Get User)
6.3 Update User
6.4 Delete User
Step 7: Routing the API Endpoints
In routes/routes.go, set up the routes to map the HTTP requests to the respective handler functions:
Step 8: Running the Application
In main.go, set up the server to start the API:
Step 9: Writing Unit Tests
Unit tests are crucial to ensure your API works as expected. In the tests folder, create user_test.go to test the handlers.
Conclusion
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.