--PROG progs/cat_rot13.go /type.Rot13/ /end.of.Rot13/
-(The "rot13" function called on line 39 is trivial and not worth reproducing.)
+(The "rot13" function called on line 38 is trivial and not worth reproducing.)
To use the new feature, we define a flag:
--PROG progs/cat_rot13.go /func.cat/ /^}/
+(We could also do the wrapping in "main" and leave "cat()" mostly alone, except
+for changing the type of the argument.)
Lines 53 and 54 set it all up: If the "rot13" flag is true, wrap the "Reader"
we received into a "Rot13" and proceed. Note that the interface variables
are values, not pointers: the argument is of type "Reader", not "*Reader",
</pre>
Fans of dependency injection may take cheer from how easily interfaces
-made substituting the implementation of a file descriptor.
+allow us to substitute the implementation of a file descriptor.
Interfaces are a distinct feature of Go. An interface is implemented by a
type if the type implements all the methods declared in the interface.
writing an actual multiplexer. The trick is to send the server a channel in the message,
which it will then use to reply to the original sender.
A realistic client-server program is a lot of code, so here is a very simple substitute
-to illustrate the idea. It starts by defining "Request" type, which embeds a channel
+to illustrate the idea. It starts by defining a "Request" type, which embeds a channel
that will be used for the reply.
--PROG progs/server.go /type.Request/ /^}/
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
-func Filter(in *chan int, out *chan int, prime int) {
+func Filter(in, out *chan int, prime int) {
for {
i := <-in // Receive value of new variable 'i' from 'in'.
if i % prime != 0 {