Integrating MySQL and Go
Let's assume you are a developer and want to save your application data in a MySQL database. As a first step, you have to establish a connection between your application and MySQL, which we will cover in this recipe.
Getting ready…
Verify whether MySQL is installed and running locally on port 3306
by executing the following command:
$ ps -ef | grep 3306
This should return the following response:

Also, log into the MySQL database and create a mydb database, executing the commands as shown in the following screenshot:

How to do it…
- Install the
github.com/go-sql-driver/mysql
package, using thego get
command, as follows:
$ go get github.com/go-sql-driver/mysql
- Create
connect-mysql.go
. Then we connect to the MySQL database and perform aSELECT
query to get the current database name, as follows:
package main import ( "database/sql" "fmt" "log" "net/http" "github.com/go-sql-driver/mysql" ) const ( CONN_HOST = "localhost" CONN_PORT = "8080" DRIVER_NAME = "mysql...