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 it works…

Once we run the program, the WebSocket server will start locally listening on port 8080

Let’s understand the program we have written:

  1. We used import ("log" "net/http" "github.com/gorilla/websocket") which is a preprocessor command that tells the Go compiler to include all files from the log, net/http, and github.com/gorilla/websocket packages.
  2. Using var clients = make(map[*websocket.Conn]bool), we created a map that represents the clients connected to a WebSocket server with KeyType as a WebSocket connection object and ValueType as Boolean.
  3. Using var broadcast = make(chan Message), we created a channel where all the received messages are written.
  4. Next, we defined a HandleClients handler, which upon receiving the HTTP GET ...

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