The Not Invented Here Bias

December 11, 2013

There is a famous Toothbrush Principle of scientific theories:

A theory is like a toothbrush: Everyone wants one, everyone has one, but nobody wants to use someone else's.

We could replace theory with development framework and describe software development to a T.

Behavioral Economist Dan Ariely...

Go Get Notified When an HTTP Request Closes

December 11, 2013

Most web requests by design take only a few dozen milliseconds to process. But sometimes web apps need to leave a connection open for a longer period of time. And sometimes the remote client closes the connection before the server has had time to respond.

On a Go-based webserver, you can receive...

The Ikea Effect and Software Development

December 10, 2013

In his book The Upside of Irrationality, behavioral economist Dan Ariely discusses a cognitive bias known as "The Ikea Effect." Stated roughly:

We ascribe more affection (and hence more value) to things that we have labored over.

Named after the popularity of Ikea's assemble-it-yourself furniture...

Installing Go 1.2 on Ubuntu 13.10

December 2, 2013

Newer Ubuntu distributions ship with various versions of the Go language. Ubuntu 13.10, for instance, has an available golang-1.1.2 package. But the most recent version of Go is 1.2.

The easiest way to install Go 1.2 on your system is by using the godeb tool. Here's how. <!--break-->

Overview

Using Custom Template Functions in Go

November 23, 2013

The Go language comes with a powerful built-in template engine. In this article I show how to add custom template functions (functions you can call from within a template).

In an earlier post I showed one way of creating a web application in Go. There I added support for templates. At its most basic, adding a bundle of templates looks like this:

import (
    "os"
    "html/template"
)

func main() {

    // Create templates
    tpl := template.Must(template.New("main").ParseGlob("*.html"))

}

The main line above creates a new bundle of templates by parsing all of the files that match the patterh *.html.

Suppose we begin with a template called index.html that looks like this:

<!DOCTYPE html>
<html>
  <head>
    <title>{{.Title}}</title>
  </head>
  <body>
    <h1>{{.Title}}</h1>
    {{.Content}}
  </body>
</html>

In this template there are two template directives: {{.Title}} (which appears twice) and {{.Content}}.

We can very quickly turn the code above into a simple template renderer by adding this:

tplVars := map[string]string {
    "Title": "Hello world",
    "Content": "Hi there",
}

tpl.ExecuteTemplate(os.StdOut, "index.html", tplVars)

If we were to run this (go run main.go), we would get output like this:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello world</title>
  </head>
  <body>
    <h1>Hello world</h1>
    Hi there
  </body>
</html>

But let's say that we wanted to do a little additional formatting. Generally, the right place to handle presentation logic is in the template. And Go provides some basic tools for this. But say we wanted to make sure that our title was always in "Title Case". There's no built-in template function for this, even though there is a function in strings that does this.

It would be convenient to be able to execute strings.Title inside of a template. While we can't do that by default, adding this feature is pretty easy. We just add a function map to the template renderer.

Here's the new code:

package main

import (
    "html/template"
    "os"
    "strings"
)

func main() {

    funcMap := template.FuncMap {
        "title": strings.Title,
    }

    tpl := template.Must(template.New("main").Funcs(funcMap).ParseGlob("*.html"))
    tplVars := map[string]string {
        "Title": "Hello world",
        "Content": "Hi there",
    }
    tpl.ExecuteTemplate(os.Stdout, "index.html", tplVars)
}

We've built out a new template function map (template.FuncMap, actually just a map[string]interface{}), and we've declared one new template function called title. When executed, title just calls strings.Title.

In order to tell the template engine about our new functions, we have to pass the function map in, and we have to do it before we parse the template files:

    tpl := template.Must(template.New("main").Funcs(funcMap).ParseGlob("*.html"))

Now let's adjust our template to use just this new function:

<!DOCTYPE html>
<html>
  <head>
    <title>{{.Title | title}}</title>
  </head>
  <body>
    <h1>{{.Title}}</h1>
    {{.Content}}
  </body>
</html>

Notice that we use the new function on the fourth line, but not later on. Now when we run our program, the output looks like this:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello world</h1>
    Hi there
  </body>
</html>

Only the first .Title was transformed into title case, since we only executed the template function title on that first instance.

You can build your own template functions, too. You're not restricted just to existing functions:

    funcMap := template.FuncMap {
        "title": strings.Title,
        "tableflip": func () string { return "(╯°□°)╯︵ ┻━┻" },
    }

Now we've added a tableflip function. Each time we embed {{tableflip}} in our template, it will produce the string (╯°□°)╯︵ ┻━┻.

That's how you can add custom template functions to Go templates. There were a number of "basic" functions that I wanted, so I created a package that collects them. Sprig has a dozen or two utility functions for handling dates, formatting, and basic integer math. (Feel free to contribute your additions via pull request!)

An Overview of Fortissimo Commons

November 22, 2013

Fortissimo is a chain of command (CoCo) framework written in PHP. Its main goal is to enable developers to write highly re-usable component-based web applications. Recently we added a new GitHub project, Fortissimo Commons, to hold dozens of utilities that we use over and over again with Fortissimo...

Creating a Cookoo Web App (in 23 Lines of Code)

November 22, 2013

Cookoo is a Chain-of-Command (CoCo) app-building framework written in Go. In this article I explain how to quickly build a Go web server using Cookoo.

By the end of this article we will have a web server with a template engine, and we'll know how to go from here to a more complex web app.

Setup

Java-compatible UUIDs in Go

November 20, 2013

Java comes with basic built-in UUID support. The java.util.UUID provides a smattering of UUID functionality. But the documentaiton is scant, and it can be frustrating to try to achieve compatibility with other languages.

Here's how to generate compatible UUIDs with Go. <!-- break -->

Java provides...

Why One Philosopher Left Academia

November 18, 2013

Zachary Ernst has written two great posts about his decision to leave academic philosophy and join a tech startup. One post focuses on giving up tenure. But the more interesting one is his explanation of why he is leaving academia. <!-- break -->

I never made it anywhere near tenure, but I did choose...

Migrating from Drupal to Middleman

November 17, 2013

Drupal.org finally moved from Drupal 6 to Drupal 7. I figured it was time for me to get this site migrated off of D6, too. But Drupal 7 was not the best option for me. Here are some of the reasons I decided to switch from Drupal:

  • Converting from Drupal 6 to 7 is a huge, huge task. It's much more...