Creating a web server in Go

Go allows you to create web servers on your own using some of the functions of its standard library.

Although a web server programmed in Go can do many things efficiently and securely, if what you really need is a powerful web server that will support modules, multiple websites, and virtual hosts, then you would be better off using a web server such as Apache, Nginx or Candy, which is written in Go.

The name of the Go program for this example will be www.go, and it will be presented in five parts. The first part of www.go contains the expected import statements:

package main 
 
import ( 
    "fmt" 
    "net/http" 
    "os" 
    "time" 
) 

The time package is not necessary for a web server to operate. However, it is needed in this case ...

Get Mastering Go - Second Edition 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.