How to Connect Golang with MySQL

Today, I will describe MySQL database connectivity with golang. MySQL is most popular open source relational database. I will let you know step by step how to golang connect with MySQL database.

I assumed you have configured golang environment within your system, if not please configure golang environment into your system.

The golang have MySQL package that help to connect MySQL with golang. We will use that package and configure db connectivity.

We will create following files into this MySQL golang example –

  • main.go : This is main go application entry file and have method.
  • db/db_connection.go : This file will have database connection method and return MySQL connection object.

Simple Golang MySQL example

We will create 'mysql_example_golang' folder under my golang work path D:\go-work.Let’s create main.go file into this folder D:\go-work\mysql_example_golang\main.go.

I have created 'test' database into MySQL database server with db username and password.We will use this information later on into this tutorial.

Golang MySQL MySQL Package

There are a couple of drivers available to support MySQL in Go. We will use github.com/go-sql-driver/mysql which have implemented the database/sql interface.

We will create db_connection.go file for database connection into db folder.We will use base SQL package 'database/sql' with MySQL driver.Let’s add below code into db_connection.go file.

Related Post
package db
"database/sql"
_"github.com/go-sql-driver/mysql"
/*Create mysql connection*/func CreateCon() *sql.DB {
db, err := sql.Open("mysql", "root:@tcp(localhost:3306)/test")
if err != nil {
fmt.Println(err.Error())
fmt.Println("db is connected")
//defer db.Close()
// make sure connection is available
err = db.Ping()
fmt.Println(err)
if err != nil {
fmt.Println("MySQL db is not connected")
fmt.Println(err.Error())

As shown above file, We have passed username:root, hostname:localhost, port:3306 and dbname:test. You need to change as per you MySQL database details.

We are creating MySQL database connection using open() method, Now we are checking open() method return error or db object, if db object then print message into console ‘MySQL db is connected’.

How To Use MySQL Connection Object into Golang APP

We have created MySQL db connect and will use into golang main.go file, we will import ‘db’ package into main app file and use CreateCon() method.

package main
    "fmt"
    "mysql_example_golang/db"
func main() {
var con *sql.DB
    fmt.Println("phpflow.com – Go MySQL Tutorial")
con := db.CreateCon();

As you can see, I have create a con variable which is DB type, since CreateCon() method return DB type variable.

How to Run Golang Application

Now, cd to the location of main.go file D:\go-work\mysql_example_golang\ and run below command to run go program.
$ go get
$ go run main.go

The go get command will download third party package libs into your work-space, here is – mysql driver package.

if every thing fine, then you will get “MySQL db is connected” message into console.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories