Multiplexing
This lesson brings attention to the client-server model that best utilizes the goroutines and channels. It provides the code and explanation of how goroutines and channels together make a client-server application.
We'll cover the following...
A typical client-server pattern
Client-server applications are the kind of applications where goroutines and channels shine. A client can be any running program on any device that needs something from a server, so it sends a request. The server receives this request, does some work, and then sends a response back to the client. In a typical situation, there are many clients (so many requests) and one (or a few) server(s). An example we use all the time is the client browser, which requests a web page. A web server responds by sending the web page back to the browser.
In Go, a server will typically perform a response to a client in a goroutine, so a goroutine is launched for every client-request. A technique commonly used is that the client-request itself contains a channel, which the server uses to send in its response.
For example, the request
is a struct like the following which embeds a reply
channel:
type Request struct {
a, b int;
replyc chan int; // reply channel
...