DEV Community

EvolveDev
EvolveDev

Posted on

Creating a CRUD Application with Golang and MySQL

Welcome to our beginner-friendly guide on building a CRUD (Create, Read, Update, Delete) application using Golang and MySQL. In this tutorial, we'll walk through the process step by step, including setting up the environment, creating a professional folder structure, and writing code examples.

Prerequisites

Before we begin, ensure you have the following installed on your system:

  • Golang: Download and install Golang from the official website.
  • MySQL: Install MySQL server and client on your machine. You can download MySQL from the official website.

Setting up the Environment

Once you have Golang and MySQL installed, let's set up our project environment.

  1. Create a New Project Folder: Open your terminal and create a new folder for your project. You can name it whatever you like. For example:
   mkdir go-crud-app
   cd go-crud-app
Enter fullscreen mode Exit fullscreen mode
  1. Initialize Go Modules: Go modules help manage dependencies for your project. Run the following command to initialize Go modules in your project folder:
   go mod init github.com/your-username/go-crud-app
Enter fullscreen mode Exit fullscreen mode
  1. Install Required Packages: We'll need a MySQL driver package for Go. Install it using the following command:
   go get -u github.com/go-sql-driver/mysql
Enter fullscreen mode Exit fullscreen mode

Folder Structure

It's important to maintain a clean and professional folder structure for your project. Here's how you can structure your project folder:

go-crud-app/
├── README.md
├── cmd/
│   └── main.go
├── internal/
│   ├── config/
│   │   └── config.go
│   ├── handlers/
│   │   └── handlers.go
│   ├── models/
│   │   └── user.go
│   └── repository/
│       └── user_repository.go
└── go.mod
Enter fullscreen mode Exit fullscreen mode
  • cmd/: Contains the main entry point of our application.
  • internal/: Contains the internal packages and modules of our application.
    • config/: Manages application configurations.
    • handlers/: Handles HTTP requests.
    • models/: Defines data models.
    • repository/: Handles database operations.

Writing the Code

Now let's write the code for our CRUD application.

  1. Configure MySQL Connection: Open config/config.go and define your MySQL connection details.
package config

const (
    DBUser     = "your_username"
    DBPassword = "your_password"
    DBName     = "your_database_name"
)
Enter fullscreen mode Exit fullscreen mode
  1. Define Data Models: Create models/user.go to define our user model.
package models

type User struct {
    ID       int
    Username string
    Email    string
}
Enter fullscreen mode Exit fullscreen mode
  1. Repository Layer: Implement CRUD operations in repository/user_repository.go.
package repository

import (
    "database/sql"
    "github.com/your-username/go-crud-app/internal/config"
    "github.com/your-username/go-crud-app/internal/models"
)

func CreateUser(user *models.User) error {
    // Implement SQL INSERT operation
}

func GetUserByID(id int) (*models.User, error) {
    // Implement SQL SELECT operation
}

// Implement UpdateUser and DeleteUser functions similarly
Enter fullscreen mode Exit fullscreen mode
  1. Handlers: Create HTTP request handlers in handlers/handlers.go.
package handlers

import (
    "net/http"
    "github.com/your-username/go-crud-app/internal/models"
)

func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
    // Implement logic to create a new user
}

func GetUserHandler(w http.ResponseWriter, r *http.Request) {
    // Implement logic to get a user by ID
}

// Implement UpdateUserHandler and DeleteUserHandler functions similarly
Enter fullscreen mode Exit fullscreen mode
  1. Main Entry Point: In cmd/main.go, set up HTTP routes and server initialization.
package main

import (
    "net/http"
    "github.com/your-username/go-crud-app/internal/handlers"
)

func main() {
    // Define HTTP routes
    http.HandleFunc("/users/create", handlers.CreateUserHandler)
    http.HandleFunc("/users/{id}", handlers.GetUserHandler)
    // Define other routes

    // Start HTTP server
    http.ListenAndServe(":8080", nil)
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You've successfully created a CRUD application using Golang and MySQL. This tutorial covered setting up the environment, creating a professional folder structure, and writing code examples for each component of the application.

Feel free to expand upon this project by adding more features, implementing authentication, or improving error handling. Happy coding!

Top comments (0)