concept message in category go

This is an excerpt from Manning's book Go in Practice.
The homePage function handles both the / path and any case where a page isn’t found
. To decide whether to return a 404 Page Not Found message or home page content, the http.Request.Path needs to be checked
. The http package contains a NotFound helper function that can optionally be used to set the response HTTP code to 404 and send the text 404 page not found.
Listing 5.12. The message struct
type Message struct { // ... } func (m *Message) Send(email, subject string, body []byte) error { // ... return nil }This describes some kind of message-sending system. In your code, you use that library to send a message from your application. In the course of writing your tests, you want to ensure that the code that sends the message is being called, but you don’t want to send the message. One way to gracefully deal with this is to write your own interface that describes the methods shown in listing 5.12, and have your code use that interface in its declarations instead of directly using the Message type, as the following listing shows.
Because you’ve created an abstraction from Message to Messager, you can easily write a mock and use that for your testing, as shown in the next listing.
We have a few things to point out about this example. The first is that the error messages are all sent to Standard Error, regardless of whether the message is an actual error or an informational message. If you glance at the godocs for the log package, you’ll notice that it doesn’t distinguish between message types. But it does make a second distinction, and that leads us to our other point.
Your application first opens a TCP connection, then sends messages, then closes the connection. But something you don’t see in your code is the response from the logger. This is because things are going on at the TCP layer that don’t bubble up into the application code.
. To decide whether to return a 404 Page Not Found message or home page content, the http.Request.Path needs to be checked
. The http package contains a NotFound helper function that can optionally be used to set the response HTTP code to 404 and send the text 404 page not found.