stencil

package module
Version: v0.0.0-...-a81b4a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2017 License: Apache-2.0 Imports: 13 Imported by: 0

README

Stencil - Simple code templating for Go

Stencil generates specialized versions of Go packages by replacing types. This is a prototype of this proposal. It will have bugs and only supports a subset of the features in the proposal.

Given a package with an interface A, stencil can generate a new package with all uses of A replaced by int (or any other type). The generated package is stored in the closest vendor directory of the repo. If no such directory exists, one is created.

Installation

Download stencil using

go get -u github.com/sridharv/stencil

Install with

go install github.com/sridharv/stencil/cmd/stencil

Usage

Detailed documentation is at GoDoc

Example Walkthrough

Install stencil as shown above. Then create a package to act as a stencil. Let's create a generic Sum method.

Create $GOPATH/src/example/stencil/math/math.go (or a package of your choice) containing

package math

func Sum(n...float64) float64 {
    var r float64
    for _, v := range n {
        r += v
    }
    return r
}

Now use it to compute the sum of ints by using stencil to replace float64 with int.

Create $GOPATH/src/example/usestencil/main.go (or another package of your choice) containing

package main

//go:generate stencil
import (
    "fmt"
    int_math "example/stencil/math/float64/int"
)

// PrintIntSum prints the sum of all elements of v to stdout.
func main() {
    ints := []int{1, 2, 3, 4, 5}
    fmt.Println("Sum of", v, "=", int_math.Sum(ints...))
}

Now run

 # Or the path to the main package you created
 cd $GOPATH/src/example/usestencil/ 

and then

go generate

Take a look at $GOPATH/src/example/usestencil/. It will have a vendor directory with a specialised version of example/stencil/math. You can now build and run the example with

go run main.go

Libraries

A few useful packages that lend themselves to being used with stencil.

  • github.com/sridharv/stencil/std/num - Max, Min and Sum for numbers. GoDoc
  • github.com/sridharv/stencil/std/slice - Slice utilities. GoDoc

License

Stencil is available under the Apache License. See the LICENSE file for details.

Contributing

Pull requests are always welcome! Please ensure any changes you send have an accompanying test case.

Documentation

Overview

Package stencil generates specialized versions of Go packages by replacing types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

func Process(paths []string, format bool) error

Process process paths, generating vendored, specialized code for any stencil import paths. If format is true any go files in paths are processed using goimports.

For detailed documentation consult the docs for "github.com/sridharv/stencil/cmd/stencil"

Types

This section is empty.

Source Files

Directories

Path Synopsis
cmd
stencil
Command stencil generates specialized versions of Go packages by replacing types.
Command stencil generates specialized versions of Go packages by replacing types.
examples
std
num
Package num provides numeric utilities intended to be used with stencil As an example, to use a version of num specialized for int32 use import "github.com/sridharv/stencil/std/num/Number/int32"
Package num provides numeric utilities intended to be used with stencil As an example, to use a version of num specialized for int32 use import "github.com/sridharv/stencil/std/num/Number/int32"
slice
Package slice implements operations on slices.
Package slice implements operations on slices.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL