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 to do it…

  1. Install the github.com/go-sql-driver/mysql and github.com/gorilla/mux packages using the go get command, as follows:
$ go get github.com/go-sql-driver/mysql$ go get github.com/gorilla/mux
  1. Create read-record-mysql.go where we connect to the MySQL database, perform a SELECT query to get all the employees from the database, iterate over the records, copy its value into the struct, add all of them to a list, and write it to an HTTP response stream, as follows:
package mainimport (  "database/sql" "encoding/json"  "log"  "net/http"  "github.com/go-sql-driver/mysql"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080"  DRIVER_NAME = "mysql"  DATA_SOURCE_NAME = "root:password@/mydb")var db *sql.DBvar connectionError ...

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