O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

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

A simple web server

Go provides us with an inbuilt library for building web servers, net/http. For every endpoint we want to create on our server, we have to do two things:

  1. Create a handler function for the endpoint, which accepts two parameters, one for writing to response and one to handle the incoming Request.
  2. Register the endpoint using net/http.HandleFunc.

The following is a simple web server that accepts all incoming requests, logs them on to the console, and then returns a Hello, World! message.

// helloServer.go package main import ( "fmt" "log" "net/http" ) func helloWorldHandler(w http.ResponseWriter, r *http.Request) { msg := fmt.Sprintf("Received request [%s] for path: [%s]", r.Method, r.URL.Path) log.Println(msg) response := ...

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