O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 dependency ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required