Simple CRUD (Create, Read, Update, Delete) operations for managing books using Go and MySQL.
- Go: Download Go
- MySQL: Download MySQL
- Clone the repository:
git clone https://github.com/your-username/Crud-Go-MySQL.git
cd Crud-Go-MySQL
- Create
.env
file:
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_NAME=
SERVER_PORT=
- Install dependencies:
go mod tidy
- Setup MySQL Database:
CREATE DATABASE crud_golang;
USE crud_golang;
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
author VARCHAR(255),
price DECIMAL(10, 2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Run:
go run cmd/main.go
Create a book:
{
"title": "The Go Programming Language",
"author": "Alan A. A. Donovan",
"price": 40.99
}
Get all books
Get book by ID
Update a book:
{
"title": "The Go Programming Language - Updated",
"author": "Alan A. A. Donovan",
"price": 45.00
}
Delete a book
Crud-Go-MySQL/
├── cmd/
│ └── main.go
├── config/
│ └── config.go
├── internal/
│ ├── database/
│ │ └── db.go
│ ├── handlers/
│ │ └── book_handler.go
│ ├── repository/
│ │ └── book_repository.go
│ └── routes/
│ └── route.go
├── models/
│ └── book.go
├── .env
└── README.md
module github.com/boysimon10/go-mysql-crud
go 1.23.2
require (
github.com/go-sql-driver/mysql v1.8.1
github.com/gorilla/mux v1.8.1
github.com/joho/godotenv v1.5.1
)