Building Restful applications using HTML tags is one of the popular ways to build web services as they follow simple and scalable architecture. CRUD, which stands for Create, Read, Update, and Delete is a common task in software development. The ability to perform CRUD operations is fundamental in any application development. In this tutorial, we will explore how to build a simple CRUD application using GoLang (Golang) and MySQL. GoLang is a popular programming language known for its efficiency and simplicity, while MySQL is a widely used relational database management system. We will start by setting up the development environment and creating the database schema. Once we have the schema in place, we will make the API endpoints for performing CRUD operations. Finally, we will test the application and ensure that everything is working as expected. So without further ado, let’s get to it.
Setting up Golang and MySQL
We will start the project by setting up our development environment, creating our database, and connecting the database to the Golang application.
Initialize Golang project
The first step is to set up the development environment and start a Golang project to build the CRUD application. Follow the steps below to set up a new Golang project.
1. Create a repository you would like to host your project and copy the link to the repository. The link should be in this format: github.com/USERNAME/REPO_NAME
2. After the repository has been properly set up, create a directory for the project and cd into the project folder by running the following command:
“`
mkdir GolangCRUD && cd GolangCrud
“`
3. After that has been completed, you can open your computer terminal to initialize a Golang project with the repo link that was created earlier, using the command below:
“`
go mod init github.com/USERNAME/REPO_NAME
“`
4. This should generate a go.mod file with the URL you just wrote and the Golang version you are using. Finally, create a file where all your Golang code will be written. At the top of the new Golang file, indicate the package name of your library. With that, you can edit the Go file to create your package and start building out the CRUD functions.
Creating the database schema
We are going to be building an application that stores a list of users in a MySQL database and then the application retrieves it when it is needed or requested. The database is going to take in both the user and their email. Before we add the MySQL driver package to our Golang application, let’s create the database and database schema for the application. To create the database and schema, you can follow the instructions below:
1. Log into your database: PS: MySQL should be installed on your computer before this command can be used.
2. Create the database using SQL commands or a MySQL management tool:
“`
CREATE DATABASE gocrud_app;
“`
3. Select the newly created database.
4. Create the users table with the desired columns:
“`
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(100)
);
“`
These commands create a database named gocrud_app and a table named users with three columns: id, name, and email. The id is used as the primary key for this database table. Once you have the database and schema set up, you can proceed with installing the MySQL driver package. Go into your terminal, cd into your project directory, and install the SQL driver package using the following command:
“`
go get -u github.com/go-sql-driver/mysql
“`
This command downloads and installs the github.com/go-sql-driver/mysql package, which provides the MySQL driver for Go applications.
Defining the routes for the Golang application
The code below shows the main function, which is the entry point for the application. It begins by importing necessary packages like database/sql, encoding/json, fmt, log, net/http, strconv, and the github.com/go-sql-driver/mysql and github.com/gorilla/mux packages. The main function creates a new router using the gorilla/mux package, which will handle incoming HTTP requests. It defines four routes corresponding to the CRUD operations: POST for creating a user, GET for retrieving a user, PUT for updating a user, and DELETE for deleting a user. Each route is associated with a specific route handler function (createUserHandler, getUserHandler, updateUserHandler, and deleteUserHandler). After defining the routes, the code starts an HTTP server using http.ListenAndServe(“:8090”, r). This line of code instructs the server to listen on port 8090 and direct incoming requests to the appropriate route handler.
“`html
Building Restful applications
Building Restful applications is one of the popular ways to build web services as they follow simple and scalable architecture. CRUD, which stands for Create, Read, Update, and Delete is a common task in software development. The ability to perform CRUD operations is fundamental in any application development. In this tutorial, we will explore how to build a simple CRUD application using GoLang (Golang) and MySQL. GoLang is a popular programming language known for its efficiency and simplicity, while MySQL is a widely used relational database management system. We will start by setting up the development environment and creating the database schema. Once we have the schema in place, we will make the API endpoints for performing CRUD operations. Finally, we will test the application and ensure that everything is working as expected. So without further ado, let’s get to it.
Setting up Golang and MySQL
We will start the project by setting up our development environment, creating our database, and connecting the database to the Golang application.
Initialize Golang project
The first step is to set up the development environment and start a Golang project to build the CRUD application. Follow the steps below to set up a new Golang project.
- Create a repository you would like to host your project and copy the link to the repository. The link should be in this format: github.com/USERNAME/REPO_NAME
- After the repository has been properly set up, create a directory for the project and cd into the project folder by running the following command:
mkdir GolangCRUD && cd GolangCrud
- After that has been completed, you can open your computer terminal to initialize a Golang project with the repo link that was created earlier, using the command below:
go mod init github.com/USERNAME/REPO_NAME
- This should generate a go.mod file with the URL you just wrote and the Golang version you are using. Finally, create a file where all your Golang code will be written. At the top of the new Golang file, indicate the package name of your library. With that, you can edit the Go file to create your package and start building out the CRUD functions.
Creating the database schema
We are going to be building an application that stores a list of users in a MySQL database and then the application retrieves it when it is needed or requested. The database is going to take in both the user and their email. Before we add the MySQL driver package to our Golang application, let’s create the database and database schema for the application. To create the database and schema, you can follow the instructions below:
- Log into your database: PS: MySQL should be installed on your computer before this command can be used.
- Create the database using SQL commands or a MySQL management tool:
CREATE DATABASE gocrud_app;
- Select the newly created database.
- Create the users table with the desired columns:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(100)
);
These commands create a database named gocrud_app and a table named users with three columns: id, name, and email. The id is used as the primary key for this database table. Once you have the database and schema set up, you can proceed with installing the MySQL driver package. Go into your terminal, cd into your project directory, and install the SQL driver package using the following command:
go get -u github.com/go-sql-driver/mysql
This command downloads and installs the github.com/go-sql-driver/mysql package, which provides the MySQL driver for Go applications.
Defining the routes for the Golang application
The code below shows the main function, which is the entry point for the application. It begins by importing necessary packages like database/sql, encoding/json, fmt, log, net/http, strconv, and the github.com/go-sql-driver/mysql and github.com/gorilla/mux packages. The main function creates a new router using the gorilla/mux package, which will handle incoming HTTP requests. It defines four routes corresponding to the CRUD operations: POST for creating a user, GET for retrieving a user, PUT for updating a user, and DELETE for deleting a user. Each route is associated with a specific route handler function (createUserHandler, getUserHandler, updateUserHandler, and deleteUserHandler). After defining the routes, the code starts an HTTP server using http.ListenAndServe(“:8090”, r). This line of code instructs the server to listen on port 8090 and direct incoming requests to the appropriate route handler.
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
const (
dbDriver = "mysql"
dbUser = "
Source link