Creating a simple HTTP server
As a programmer, if you have to create a simple HTTP server then you can easily write it using Go's net/http package, which we will be covering in this recipe.
How to do it…
In this recipe, we are going to create a simple HTTP server that will render Hello World! when we browse http://localhost:8080 or execute curlhttp://localhost:8080 from the command line. Perform the following steps:
- Create http-server.goand copy the following content:
package main
import 
(
  "fmt"
  "log"
  "net/http"
)
const 
(
  CONN_HOST = "localhost"
  CONN_PORT = "8080"
)
func helloWorld(w http.ResponseWriter, r *http.Request) 
{
  fmt.Fprintf(w, "Hello World!")
}
func main() 
{
  http.HandleFunc("/", helloWorld)
  err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil)
  if err != nil 
  {
    log.Fatal("error starting http server : ", err)
    return
  }
}- Run the program with the following command:
$ go run http-server.goHow it works…
Once we run the program, an HTTP server will start locally listening on port 8080. Opening http://localhost:8080 in a browser will display Hello World! from the server, as shown in the following screenshot:

Hello World!
Let’s understand what each line in the program means:
- package main: This defines the package name of the program.
- import ( "fmt" "log" "net/http" ): This is a preprocessor command that tells the Go compiler to include all files from- fmt,- log, and the- net/httppackage.
- const ( CONN_HOST = "localhost" CONN_PORT = "8080" ): We declare constants in the Go program using the- constkeyword. Here we declared two constants—one is- CONN_HOSTwith localhost as a value and another one is- CONN_PORTwith- 8080as a value.
- func helloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World!") }: This is a Go function that takes- ResponseWriterand- Requestas an input and writes- Hello World!on an HTTP response stream.
Next, we declared the main() method from where the program execution begins, as this method does a lot of things. Let’s understand it line by line:
- http.HandleFunc("/", helloWorld): Here, we are registering the- helloWorldfunction with the- /URL pattern using- HandleFuncof the- net/httppackage, which means- helloWorldgets executed, passing- (http.ResponseWriter,- *http.Request)as a parameter to it whenever we access the HTTP URL with pattern- /.
- err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil): Here, we are calling- http.ListenAndServeto serve HTTP requests that handle each incoming connection in a separate Goroutine.- ListenAndServeaccepts two parameters—server address and handler. Here, we are passing the server address as- localhost:8080and handler as- nil, which means we are asking the server to use- DefaultServeMuxas a handler.
- if err != nil { log.Fatal("error starting http server : ", err) return}: Here, we check whether there is a problem starting the server. If there is, then log the error and exit with a status code of- 1.
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                 
        