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:
- 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.
- Next, we declared the Go data structure Person, which has Id and Name fields.