- Install the github.com/go-sql-driver/mysql package, using the go 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 a SELECT query to get the current database name, as follows:
package mainimport ( "database/sql" "fmt" "log" "net/http" "github.com/go-sql-driver/mysql")const ( CONN_HOST = "localhost" CONN_PORT = "8080" DRIVER_NAME = "mysql" DATA_SOURCE_NAME = "root:password@/mydb")var db *sql.DBvar connectionError errorfunc init() { db, connectionError = sql.Open(DRIVER_NAME, DATA_SOURCE_NAME) if connectionError != nil { log.Fatal("error connecting to database :: ", connectionError) }}func getCurrentDb(w http.ResponseWriter, r *http.Request) ...