Skip to main content
In this guide, you’ll learn how to integrate a MySQL database into your Go project using the SQL package and explore useful methods for common database operations. Follow along to set up your MySQL database, create tables, and execute queries and commands using Go.

Setting Up MySQL and Creating the Database

Begin by installing MySQL on your system and logging into the MySQL console. Once logged in, create a database named “learning” with the following commands:
Next, create a simple table called data that includes two fields:
  • An integer id as the primary key.
  • A name column of type VARCHAR(255) used for storing strings.
The data table consists of an id (primary key) and a name field. This structure is suitable for basic storage, demonstration purposes, or further expansion in your applications.

Inserting and Verifying Data

Insert some sample records into the table using the following SQL commands:
To confirm the inserted data, run a SELECT query:
Later in the project, you will add additional rows using the SQL package in Go.

Installing the SQL Package and Setting Up Your Project

To work with MySQL in Go, install the third-party driver package with:
Create a file named constants.go to store your database connection details as constants. For example, if your database is named “learning” and your username is “root”, define the constants as follows (remember to properly secure sensitive information in a real project):

Establishing a Database Connection in Go

In your main.go file, import the MySQL driver for its side effects and open a new MySQL connection:
The function sql.Open accepts a driver name and a corresponding data source name. For more details, refer to the Go documentation:
To ensure the connection is established correctly, use this helper function to check for errors:

Querying the Database

Selecting Data

To retrieve rows from the data table, use the Query method as shown below:
For more details on the Query method, see the Go documentation:
Define a struct to hold the query results:
Loop through the result set using the Next and Scan methods to print each row:
The Next method prepares the next row for reading, while Scan copies the column values into the provided variables. Their official documentation includes:

Inserting Data via Go

After validating your SELECT query, insert a new row into the table using the Exec method:
Retrieve details about the execution, such as the last inserted ID and the number of rows affected:
Verify the insertion by running another SELECT query and printing the results:
When you run the program, the expected output should be similar to:
You can also confirm the new row in the MySQL console:

Conclusion

With these instructions, you now have a solid understanding of how to integrate MySQL with Go using the SQL package. You have learned to create a database and table, insert data, and perform both SELECT and INSERT operations programmatically. Use these techniques to build robust database operations in your Go projects.
The image shows a code editor with a Go programming language file open, displaying a function and an autocomplete suggestion list for error handling. The terminal at the bottom shows a Go documentation command for SQL queries.
Always ensure that your database credentials are stored securely and consider using environment variables or secret management tools for production applications.

Watch Video