A web server
source ↗A web server
Let’s finish with a complete Go program, a web server.
This one is actually a kind of web re-server.
Google provides a service at chart.apis.google.com
that does automatic formatting of data into charts and graphs.
It’s hard to use interactively, though,
because you need to put the data into the URL as a query.
The program here provides a nicer interface to one form of data: given a short piece of text,
it calls on the chart server to produce a QR code, a matrix of boxes that encode the
text.
That image can be grabbed with your cell phone’s camera and interpreted as,
for instance, a URL, saving you typing the URL into the phone’s tiny keyboard.
Here’s the complete program. An explanation follows.
The pieces up to main should be easy to follow.
The one flag sets a default HTTP port for our server. The template
variable templ is where the fun happens. It builds an HTML template
that will be executed by the server to display the page; more about
that in a moment.
The main function parses the flags and, using the mechanism
we talked about above, binds the function QR to the root path
for the server. Then http.ListenAndServe is called to start the
server; it blocks while the server runs.
QR just receives the request, which contains form data, and
executes the template on the data in the form value named s.
The template package html/template is powerful;
this program just touches on its capabilities.
In essence, it rewrites a piece of HTML text on the fly by substituting elements derived
from data items passed to templ.Execute, in this case the
form value.
Within the template text (templateStr),
double-brace-delimited pieces denote template actions.
The piece from {{"{{if .}}"}}
to {{"{{end}}"}} executes only if the value of the current data item, called . (dot),
is non-empty.
That is, when the string is empty, this piece of the template is suppressed.
The two snippets {{"{{.}}"}} say to show the data presented to
the template—the query string—on the web page.
The HTML template package automatically provides appropriate escaping so the
text is safe to display.
The rest of the template string is just the HTML to show when the page loads. If this is too quick an explanation, see the documentation for the template package for a more thorough discussion.
And there you have it: a useful web server in a few lines of code plus some data-driven HTML text. Go is powerful enough to make a lot happen in a few lines.