Skip to content

boysimon10/go-mysql-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRUD Operations with Go and MySQL

Simple CRUD (Create, Read, Update, Delete) operations for managing books using Go and MySQL.

Prerequisites

Installation

  1. Clone the repository:
git clone https://github.com/your-username/Crud-Go-MySQL.git
cd Crud-Go-MySQL
  1. Create .env file:
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_NAME=
SERVER_PORT=
  1. Install dependencies:
go mod tidy
  1. 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
);
  1. Run:
go run cmd/main.go

API Endpoints

POST /books

Create a book:

{
  "title": "The Go Programming Language",
  "author": "Alan A. A. Donovan",
  "price": 40.99
}

GET /books

Get all books

GET /books/{id}

Get book by ID

PUT /books/{id}

Update a book:

{
  "title": "The Go Programming Language - Updated",
  "author": "Alan A. A. Donovan",
  "price": 45.00
}

DELETE /books/{id}

Delete a book

Project Structure

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

Dependencies

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
)

About

Simple CRUD operation using Go x MySQL

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages