Hello, I have created a simple Go project that uses Gofr, Gorm, and MySQL.
This project aims to create REST APIs for basic operations, including create, read, update, and delete. I have create simple example involving a student management using these operations to illustrate the use case.
This project can be consider for future modification for learning purpose where i can add more feature for a student such as subjects a student can have.
-
create database students;
-
use database students;
-
CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(255), class VARCHAR(50), age INT );
-
After that i have used create API and generated students records.
- Install Go version - 1.21.5 via
- Once installed, Please check via below command
go version
- Install GoFr
go get gofr.dev
- Clone the Project
git clone <project_url>
cd <project_directory>
- Install Dependencies (Make sure that Go and MySQL are installed on your computer)
go mod tidy
- Create a Local MySQL Database
mysql -u root -p
- Run the Program
go run main.go
- Test with Postman
HTTP-Rest-API/
├── configs/
│ └── .env
│
├── handler/
│ └── handler.go
│
├── model/
│ └── model.go
│
├── datastore/
│ ├── datastore.go
│ └── interface.go
│── test
│ ├── datastore_test.go
│ ├── handler_test.go
|
├── main.go
├── go.mod
After our project is succesfully built we have run it with the command go run main.go
and it then pops the result of successfully connected to the database.
Then we have used postman and tested different apis that we have made, and got the output similar to the attached images below: