Skip to main content
In this lesson, we’ll create a simple project that connects to a MySQL database using Golang. You will learn how to create a database, set up a table, insert sample data, and build a Golang application that connects to the database and registers HTTP routes. ─────────────────────────────

Step 1: Initial Golang File Overview

Begin by creating a basic Golang file. The snippet below includes the necessary imports and a placeholder function for error checking:
─────────────────────────────

Step 2: Database Setup in MySQL

First, create a database named Inventory. Follow these steps:
  1. Log in to MySQL:
  2. In the MySQL shell, create the database:
  3. Switch to the new database and create a table named Products with the following columns:
    • id: An integer, not null and auto-incremented.
    • name: A non-null varchar field.
    • quantity: An integer.
    • price: A float with precision.
    Execute the following SQL commands:
─────────────────────────────

Step 3: Project Directory and Module Initialization

Set up your workspace by creating a new project directory called my-inventory. Then, initialize a new Go module and create the main application file.
Initialize the module and create the app file:
─────────────────────────────

Step 4: Application Structure and Database Initialization

Open the app.go file in your preferred IDE. This file will hold application variables such as the HTTP router, database instance, and methods for setting up routes.
  1. Define the Package and App Struct Start by declaring the package and defining an App struct that stores pointers to the HTTP router and the SQL database:
  2. Create a Constants File for Database Configuration For better organization, create a file named constants.go that will store your database configuration details:
  3. Implement the Initialize Method This method constructs the connection string, opens a MySQL connection, and initializes the HTTP router:
  4. Create the Run Method Define the Run method to start the HTTP server. This method listens on a specified address and uses log.Fatal to report any server startup errors:
─────────────────────────────

Step 5: Main Function and Route Handling

Create a main.go file, which will serve as the application’s entry point.
  1. Set Up the Main Function Initialize the App, set up your HTTP routes, and run the server on a specified address (e.g., “localhost:10000”):
  2. Register HTTP Routes Define the route registration method on the App struct. Here, a simple GET route for /products is registered, which is handled by the getProducts function:
Remember to implement the getProducts handler to process HTTP GET requests for the /products route based on your application needs.
─────────────────────────────

Summary

In this lesson, we covered the following:
  • Created a MySQL database and a Products table with sample data.
  • Set up a new Golang module and organized the project into multiple files.
  • Built an App struct to encapsulate the database connection and HTTP router.
  • Implemented the Initialize and Run methods to manage the MySQL connection and start the HTTP server.
  • Registered a sample route for fetching products.
This structured approach lays the foundation for further developing your application with expanded routing and enhanced database operations.

Watch Video