How it works...

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

Browsing to http://localhost:8080/employees will list all the records from the employee table as shown in the following screenshot:

Let’s look at the program we have written:

  1. Using import ( "database/sql" "encoding/json" "log" "net/http" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux"), we imported an additional package, encoding/json, which helps in marshalling the Go data structure to JSON.
  2. Next, we declared the Go data structure Person, which has Id and Name fields.
Do remember that the field name should begin with a capital ...

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.