Book Image

Go Web Development Cookbook

By : Arpit Aggarwal
Book Image

Go Web Development Cookbook

By: Arpit Aggarwal

Overview of this book

Go is an open source programming language that is designed to scale and support concurrency at the language level. This gives you the liberty to write large concurrent web applications with ease. From creating web application to deploying them on Amazon Cloud Services, this book will be your one-stop guide to learn web development in Go. The Go Web Development Cookbook teaches you how to create REST services, write microservices, and deploy Go Docker containers. Whether you are new to programming or a professional developer, this book will help get you up to speed with web development in Go. We will focus on writing modular code in Go; in-depth informative examples build the base, one step at a time. You will learn how to create a server, work with static files, SQL, NoSQL databases, and Beego. You will also learn how to create and secure REST services, and create and deploy Go web application and Go Docker containers on Amazon Cloud Services. By the end of the book, you will be able to apply the skills you've gained in Go to create and explore web applications in any domain.
Table of Contents (13 chapters)

Building a Go web application Docker image

In this recipe, we will build a Docker image that connects to the MySQL database instance running in a separate Docker container.

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 main
import
(
"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
var db *sql.DB
var connectionError error
const
(
CONN_PORT = "8080"
DRIVER_NAME = "mysql"
DATA_SOURCE_NAME = "root...