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. Create http-server.go, where we will create a simple HTTP server and a handler which will give us the current database details, such as machine IP, hostname, port, and selected database, as follows:

package mainimport (  "bytes"  "database/sql"  "fmt"  "log"  "net/http"  "github.com/go-sql-driver/mysql"  "github.com/gorilla/mux")var db *sql.DBvar connectionError errorconst (  CONN_PORT = "8080"  DRIVER_NAME = "mysql"  DATA_SOURCE_NAME = "root:my-pass@tcp(mysql-container:3306)/mysql")func init() {  db, connectionError = sql.Open(DRIVER_NAME, DATA_SOURCE_NAME)  if connectionError != nil   {    log.Fatal("error connecting to database : ", connectionError)  }}func getDBInfo(w http.ResponseWriter, r *http.Request) { rows, err := db.Query("SELECT ...

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