Switch to stable fork of ps package
1 file changed
tree: c18d06cbbcc2c62cf62ef8952b4d9700fa9ff02b
  1. .gitignore
  2. .travis.yml
  3. builder.go
  4. builder_test.go
  5. example_test.go
  6. README.md
  7. reflect.go
  8. registry.go

Builder - fluent immutable builders for Go

GoDoc Build Status

Builder was originally written for Squirrel, a fluent SQL generator. It is probably the best example of Builder in action.

Builder helps you write fluent DSLs for your libraries with method chaining:

resp := ReqBuilder.
    Header("User-Agent", "Builder").

Builder uses immutable persistent data structures (these, specifically) so that each step in your method chain can be reused:

build := WordBuilder.AddLetters("Build")
builder := build.AddLetters("er")
building := build.AddLetters("ing")

Builder makes it easy to build structs using the builder pattern (surprise!):

import "github.com/lann/builder"

type Muppet struct {
    Name string
    Friends []string

type muppetBuilder builder.Builder

func (b muppetBuilder) Name(name string) muppetBuilder {
    return builder.Set(b, "Name", name).(muppetBuilder)

func (b muppetBuilder) AddFriend(friend string) muppetBuilder {
    return builder.Append(b, "Friends", friend).(muppetBuilder)

func (b muppetBuilder) Build() Muppet {
    return builder.GetStruct(b).(Muppet)

var MuppetBuilder = builder.Register(muppetBuilder{}, Muppet{}).(muppetBuilder)
    AddFriend("Dr. Honeydew").

=> Muppet{Name:"Beaker", Friends:[]string{"Dr. Honeydew"}}


Builder is released under the MIT License.