Is there an analogue of routers (ala gorilla / gin gonic) for tcp?
1 answer
TCP is easier to imagine as io.Reader and io.Writer . Thus, reading from the connection is reading what is written there from the other end. And the record is what should be read from the other side. Those. you can write anything. Any data.
HTTP short
HTTP protocol has routing, because there is some agreement. For example, a GET request for how clean the text will look like.
GET /user/1 HTTP/1.1 At the same time, the end of the line \r\n . And some headers are possible, for example
GET /user/1 HTTP/1.1 Host: example.com Accept-Encoding: gzip deflate Authorization: Bearer token If the request has a body, then the header is separated from the body by an empty string.
TCP routing
Thus, in order to be able to route TCP requests, we need some kind of agreement on what the request is and how to encode it.
In general, it is an agreement about the message format, - its length, route, - sent via TCP. Also, it is necessary to think over the maximum allowable message sizes in order not to get memory overruns when an incorrectly composed message.
Thus, having decoded the message, you can use the route extracted from it.
There are ready-made messaging systems, via a permanent TCP connection. For example, mangos , which embodies the Nanomsg (SP - Scalable Protocol) protocol, is quite flexible, but complex.
Also, in the standard library there is a package net / textproto , which allows you to create your own text protocols.
In addition, there are various RPC - tools for calling remote procedures, such as net / rpc or gRPC .