How it works...

Once we run the program, the HTTP server will start locally listening on port 8080.

Browsing to http://localhost:8080/ will return you the current database name, as shown in the following screenshot:

Let’s understand the program we have written:

  1. Using import ( "database/sql" "fmt" "log" "net/http" _ "github.com/go-sql-driver/mysql"), we imported github.com/go-sql-driver/mysql for its side effects or initialization, using the underscore in front of an import statement explicitly.
  1. Using var db *sql.DB, we declared a private DB instance.
Depending on the project size, you can declare a DB instance globally, inject it as a ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.