Jun 26, 2017 - 5 min read
Stencil - Simple code templating for Go

Go doesn’t have user defined generics. While generics can be convenient, for much Go code, interfaces suffice. There are however, cases where the lack of generics leads to cumbersome code. The proposal below aims to handle a limited number of these cases using code generation. It builds on an idea proposed earlier in Gonerics. It does not propose a language change. Implementations will be command line tools.

The Proposal

Examples

The tool described above will be called stencil in the rest of this post.

Consider a simple Max function in $GOPATH/src/example/num/num.go

package num

// Max returns the largest number in n. If n is empty it returns 0
func Max(n...float64) float64 {
    // return the largest number in n
}

In $GOPATH/some/cmd/main.go, the largest of a slice of float32 values needs to be computed. Using num.Max directly requires cumbersome casts to float32 and an allocation of a new slice.

func main() {
    var vals []float32
    vals = getFloat32Values()
    f64s := make([]float64, len(vals))
    for i, f32 := range vals {
        f64s[i] = float64(f32)
    }
    fmt.Println("Max(", vals, ") = ", float32(num.Max(f64s...)))
}

Instead, when using stencil, directly import a float32 version of example/num as example/num/float64/float32

package main

import (
    "fmt"
    float32_num "example/num/float64/float32"
)

func main() {
    var vals []float32
    vals = getFloat32Values()
    fmt.Println("Max(", vals, ") = ", float32_num.Max(vals...))
}

Running stencil on $GOPATH/some/cmd/main.go generates $GOPATH/some/cmd/vendor/example/num/float64/float32/num.go, containing

package num

// Max returns the largest number in n. If n is empty it returns 0
func Max(n...float32) float32 {
    // return the largest number in n
}

If stencil is used as a replacement for the format-on-save command, it automatically generates the example/num/float64/float32 package when the main.go file above is saved. Alternately, stencil can be run manually or with go generate.

Channel operations

The ability to merge channels is a fairly useful and common pattern, as described in this blogpost. However, re-using the moderately complex merge function requires us to either duplicate code, or use chan interface{} and give up type safety. Using stencil, this is simplified.

Create the merge function in a package.

package concurrent

type T interface{}

func Merge(c ...<-chan T) <-chan T {
    // perform merge
}

and use it with the needed type

import (
    "fmt"
    string_chan "example/concurrent/T/string"
)

func MyCode(c1, c2, c3 <-chan string) {
    c1, c2, c3 := make(chan string), make(chan string), make(chan string)
    
    // start goroutines to send data on c1, c2 and c3
    
    // Collect the results
    merged := string_chan.Merge(c1, c2, c3)
    for str := range merged {
        fmt.Println(str)
    }
}

stencil automatically generates the example/concurrent/T/string package and places it in the vendor directory.

Analysis

On the face of it, this approach will work well in certain limited situations.

Pros

Cons

I’m confident that many of the issues above can be handled with some extra design effort. Small, self contained packages that provide simple functionality seem to lend themselves well to stencilling. Typed data structures, algorithms, database access and serialisation come to mind as areas where this approach will prove useful. However, I do not expect it to be useful in any attempts to write heavily functional code or provide different error handling mechanisms.

The evolution of this proposal can be tracked in this Github issue.

Prototype

Ideas need to be tested with actual code before they can be deemed useful. I’ve built a simple prototype of this idea at github.com/sridharv/stencil. It is currently limited to numeric, boolean, string types and the empty interface. It doesn’t support stencilling using custom types and most likely contains bugs. It also doesn’t perform any type inference. However, it should be useful to explore the practicality and usefulness of this idea. Please try it out and file issues as you encounter them.