jen

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2017 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Code

type Code interface {
	// contains filtered or unexported methods
}

type File

type File struct {
	*Group

	// If you're worried about package aliases conflicting with local variable
	// names, you can set a prefix here. Package foo becomes {prefix}_foo.
	PackagePrefix string
	// contains filtered or unexported fields
}

func NewFile

func NewFile(name string) *File
Example
f := NewFile("main")
f.Func().Id("main").Params().Block(
	Qual("fmt", "Println").Call(
		Lit("Hello, world"),
	),
)
fmt.Printf("%#v", f)
Output:

package main

import fmt "fmt"

func main() {
	fmt.Println("Hello, world")
}

func NewFilePath

func NewFilePath(path string) *File
Example
f := NewFilePath("a.b/c")
f.Func().Id("init").Params().Block(
	Qual("a.b/c", "Local").Call(),
	Qual("d.e/f", "Remote").Call(),
	Qual("g.h/f", "Collision").Call(),
)
fmt.Printf("%#v", f)
Output:

package c

import (
	f "d.e/f"
	f1 "g.h/f"
)

func init() {
	Local()
	f.Remote()
	f1.Collision()
}

func NewFilePathName

func NewFilePathName(path, name string) *File
Example
f := NewFilePathName("a.b/c", "main")
f.Func().Id("main").Params().Block(
	Qual("a.b/c", "Foo").Call(),
)
fmt.Printf("%#v", f)
Output:

package main

func main() {
	Foo()
}

func (*File) Anon

func (f *File) Anon(paths ...string)
Example
f := NewFile("c")
f.Anon("a")
f.Func().Id("init").Params().Block()
fmt.Printf("%#v", f)
Output:

package c

import _ "a"

func init() {}

func (*File) GoString

func (f *File) GoString() string

func (*File) PackageComment

func (f *File) PackageComment(comment string)
Example
f := NewFile("c")
f.PackageComment("a")
f.PackageComment("b")
f.Func().Id("init").Params().Block()
fmt.Printf("%#v", f)
Output:

// a
// b
package c

func init() {}

func (*File) Render

func (f *File) Render(w io.Writer) error
Example
f := NewFile("a")
f.Func().Id("main").Params().Block()
buf := &bytes.Buffer{}
err := f.Render(buf)
if err != nil {
	fmt.Println(err.Error())
} else {
	fmt.Println(buf.String())
}
Output:

package a

func main() {}

func (*File) Save

func (f *File) Save(filename string) error

type Group

type Group struct {
	// contains filtered or unexported fields
}

func (*Group) Add

func (g *Group) Add(code ...Code) *Statement

Add creates a new statement in the group and appends the provided code.

func (*Group) Append

func (g *Group) Append(c ...Code) *Statement

Append inserts the built in function append

func (*Group) Assert

func (g *Group) Assert(c Code) *Statement

Assert inserts a type assertion

func (*Group) Block

func (g *Group) Block(c ...Code) *Statement

Block inserts curly braces containing a statement list

func (*Group) BlockFunc

func (g *Group) BlockFunc(f func(*Group)) *Statement

Block inserts curly braces containing a statement list

func (*Group) Bool

func (g *Group) Bool() *Statement

Bool inserts the bool identifier

func (*Group) Break

func (g *Group) Break() *Statement

Break inserts the break keyword

func (*Group) Byte

func (g *Group) Byte() *Statement

Byte inserts the byte identifier

func (*Group) Call

func (g *Group) Call(c ...Code) *Statement

Call inserts parenthesis containing a comma separated list

func (*Group) CallFunc

func (g *Group) CallFunc(f func(*Group)) *Statement

Call inserts parenthesis containing a comma separated list

func (*Group) Cap

func (g *Group) Cap(c ...Code) *Statement

Cap inserts the built in function cap

func (*Group) Case

func (g *Group) Case(c ...Code) *Statement

Case inserts the case keyword, followed by a comma separated list

func (*Group) CaseBlock

func (g *Group) CaseBlock(c ...Code) *Statement

CaseBlock inserts a statement list preceded by a colon

func (*Group) CaseBlockFunc

func (g *Group) CaseBlockFunc(f func(*Group)) *Statement

CaseBlock inserts a statement list preceded by a colon

func (*Group) CaseFunc

func (g *Group) CaseFunc(f func(*Group)) *Statement

Case inserts the case keyword, followed by a comma separated list

func (*Group) Chan

func (g *Group) Chan() *Statement

Chan inserts the chan keyword

func (*Group) Close

func (g *Group) Close(c ...Code) *Statement

Close inserts the built in function close

func (*Group) Comment

func (g *Group) Comment(str string) *Statement

func (*Group) Commentf

func (g *Group) Commentf(format string, a ...interface{}) *Statement

func (*Group) Complex

func (g *Group) Complex(c ...Code) *Statement

Complex inserts the built in function complex

func (*Group) Complex128

func (g *Group) Complex128() *Statement

Complex128 inserts the complex128 identifier

func (*Group) Complex64

func (g *Group) Complex64() *Statement

Complex64 inserts the complex64 identifier

func (*Group) Const

func (g *Group) Const() *Statement

Const inserts the const keyword

func (*Group) Continue

func (g *Group) Continue() *Statement

Continue inserts the continue keyword

func (*Group) Copy

func (g *Group) Copy(c ...Code) *Statement

Copy inserts the built in function copy

func (*Group) Default

func (g *Group) Default() *Statement

Default inserts the default keyword

func (*Group) Defer

func (g *Group) Defer() *Statement

Defer inserts the defer keyword

func (*Group) Defs

func (g *Group) Defs(c ...Code) *Statement

Defs inserts parenthesis containing a statement list

func (*Group) DefsFunc

func (g *Group) DefsFunc(f func(*Group)) *Statement

Defs inserts parenthesis containing a statement list

func (*Group) Delete

func (g *Group) Delete(c ...Code) *Statement

Delete inserts the built in function delete

func (*Group) Dict

func (g *Group) Dict(m map[Code]Code) *Statement

Dict inserts a map literal

func (*Group) DictFunc

func (g *Group) DictFunc(f func(map[Code]Code)) *Statement

func (*Group) Do

func (g *Group) Do(f func(*Statement)) *Statement

Do creates a new statement in the group and calls the provided function with it as a parameter

func (*Group) Else

func (g *Group) Else() *Statement

Else inserts the else keyword

func (*Group) Empty

func (g *Group) Empty() *Statement

Empty token produces no output but is followed by a separator in a list.

func (*Group) Err

func (g *Group) Err() *Statement

Err inserts the err identifier

func (*Group) Error

func (g *Group) Error() *Statement

Error inserts the error identifier

func (*Group) Fallthrough

func (g *Group) Fallthrough() *Statement

Fallthrough inserts the fallthrough keyword

func (*Group) False

func (g *Group) False() *Statement

False inserts the false identifier

func (*Group) Float32

func (g *Group) Float32() *Statement

Float32 inserts the float32 identifier

func (*Group) Float64

func (g *Group) Float64() *Statement

Float64 inserts the float64 identifier

func (*Group) For

func (g *Group) For(c ...Code) *Statement

For inserts the for keyword, followed by a semicolon separated list

func (*Group) ForFunc

func (g *Group) ForFunc(f func(*Group)) *Statement

For inserts the for keyword, followed by a semicolon separated list

func (*Group) Func

func (g *Group) Func() *Statement

Func inserts the func keyword

func (*Group) Go

func (g *Group) Go() *Statement

Go inserts the go keyword

func (*Group) GoString

func (g *Group) GoString() string

func (*Group) Goto

func (g *Group) Goto() *Statement

Goto inserts the goto keyword

func (*Group) Id

func (g *Group) Id(name string) *Statement

func (*Group) If

func (g *Group) If(c ...Code) *Statement

If inserts the if keyword, followed by a semicolon separated list

func (*Group) IfFunc

func (g *Group) IfFunc(f func(*Group)) *Statement

If inserts the if keyword, followed by a semicolon separated list

func (*Group) Imag

func (g *Group) Imag(c ...Code) *Statement

Imag inserts the built in function imag

func (*Group) Index

func (g *Group) Index(c ...Code) *Statement

Index inserts square brackets containing a colon separated list

func (*Group) IndexFunc

func (g *Group) IndexFunc(f func(*Group)) *Statement

Index inserts square brackets containing a colon separated list

func (*Group) Int

func (g *Group) Int() *Statement

Int inserts the int identifier

func (*Group) Int16

func (g *Group) Int16() *Statement

Int16 inserts the int16 identifier

func (*Group) Int32

func (g *Group) Int32() *Statement

Int32 inserts the int32 identifier

func (*Group) Int64

func (g *Group) Int64() *Statement

Int64 inserts the int64 identifier

func (*Group) Int8

func (g *Group) Int8() *Statement

Int8 inserts the int8 identifier

func (*Group) Interface

func (g *Group) Interface(c ...Code) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

func (*Group) InterfaceFunc

func (g *Group) InterfaceFunc(f func(*Group)) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

func (*Group) Iota

func (g *Group) Iota() *Statement

Iota inserts the iota identifier

func (*Group) Len

func (g *Group) Len(c ...Code) *Statement

Len inserts the built in function len

func (*Group) Line

func (g *Group) Line() *Statement

func (*Group) List

func (g *Group) List(c ...Code) *Statement

List inserts a comma separated list

func (*Group) ListFunc

func (g *Group) ListFunc(f func(*Group)) *Statement

List inserts a comma separated list

func (*Group) Lit

func (g *Group) Lit(v interface{}) *Statement

func (*Group) LitFunc

func (g *Group) LitFunc(f func() interface{}) *Statement

func (*Group) Make

func (g *Group) Make(c ...Code) *Statement

Make inserts the built in function make

func (*Group) Map

func (g *Group) Map(c Code) *Statement

Map inserts the map keyword, followed by square brackets

func (*Group) New

func (g *Group) New(c ...Code) *Statement

New inserts the built in function new

func (*Group) Nil

func (g *Group) Nil() *Statement

Nil inserts the nil identifier

func (*Group) Null

func (g *Group) Null() *Statement

Null token produces no output but also no separator in a list.

func (*Group) Op

func (g *Group) Op(op string) *Statement

func (*Group) Panic

func (g *Group) Panic(c ...Code) *Statement

Panic inserts the built in function panic

func (*Group) Params

func (g *Group) Params(c ...Code) *Statement

Params inserts parenthesis containing a comma separated list

func (*Group) ParamsFunc

func (g *Group) ParamsFunc(f func(*Group)) *Statement

Params inserts parenthesis containing a comma separated list

func (*Group) Parens

func (g *Group) Parens(c Code) *Statement

Parens inserts parenthesis

func (*Group) Print

func (g *Group) Print(c ...Code) *Statement

Print inserts the built in function print

func (*Group) Println

func (g *Group) Println(c ...Code) *Statement

Println inserts the built in function println

func (*Group) Qual

func (g *Group) Qual(path, name string) *Statement

func (*Group) Range

func (g *Group) Range() *Statement

Range inserts the range keyword

func (*Group) Real

func (g *Group) Real(c ...Code) *Statement

Real inserts the built in function real

func (*Group) Recover

func (g *Group) Recover(c ...Code) *Statement

Recover inserts the built in function recover

func (*Group) Return

func (g *Group) Return(c ...Code) *Statement

Return inserts the return keyword, followed by a comma separated list

func (*Group) ReturnFunc

func (g *Group) ReturnFunc(f func(*Group)) *Statement

Return inserts the return keyword, followed by a comma separated list

func (*Group) Rune

func (g *Group) Rune() *Statement

Rune inserts the rune identifier

func (*Group) Sel

func (g *Group) Sel(c ...Code) *Statement

Sel inserts a group of selectors - items separated by periods

func (*Group) SelFunc

func (g *Group) SelFunc(f func(*Group)) *Statement

Sel inserts a group of selectors - items separated by periods

func (*Group) Select

func (g *Group) Select() *Statement

Select inserts the select keyword

func (*Group) String

func (g *Group) String() *Statement

String inserts the string identifier

func (*Group) Struct

func (g *Group) Struct() *Statement

Struct inserts the struct keyword

func (*Group) Switch

func (g *Group) Switch(c ...Code) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

func (*Group) SwitchFunc

func (g *Group) SwitchFunc(f func(*Group)) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

func (*Group) Tag

func (g *Group) Tag(items map[string]string) *Statement

Tag adds a struct tag

func (*Group) True

func (g *Group) True() *Statement

True inserts the true identifier

func (*Group) Type

func (g *Group) Type() *Statement

Type inserts the type keyword

func (*Group) Uint

func (g *Group) Uint() *Statement

Uint inserts the uint identifier

func (*Group) Uint16

func (g *Group) Uint16() *Statement

Uint16 inserts the uint16 identifier

func (*Group) Uint32

func (g *Group) Uint32() *Statement

Uint32 inserts the uint32 identifier

func (*Group) Uint64

func (g *Group) Uint64() *Statement

Uint64 inserts the uint64 identifier

func (*Group) Uint8

func (g *Group) Uint8() *Statement

Uint8 inserts the uint8 identifier

func (*Group) Uintptr

func (g *Group) Uintptr() *Statement

Uintptr inserts the uintptr identifier

func (*Group) Values

func (g *Group) Values(c ...Code) *Statement

Values inserts curly braces containing a comma separated list

func (*Group) ValuesFunc

func (g *Group) ValuesFunc(f func(*Group)) *Statement

Values inserts curly braces containing a comma separated list

func (*Group) Var

func (g *Group) Var() *Statement

Var inserts the var keyword

type Statement

type Statement []Code

func Add

func Add(code ...Code) *Statement

Add creates a new statement and appends the provided code.

Example
a := Id("a")
i := Int()
c := Var().Add(a, i)
fmt.Printf("%#v", c)
Output:

var a int

func Append

func Append(c ...Code) *Statement

Append inserts the built in function append

Example
c := Append(Id("a"), Id("b"))
fmt.Printf("%#v", c)
Output:

append(a, b)
Example (More)
c := Id("a").Op("=").Append(Id("a"), Id("b").Op("..."))
fmt.Printf("%#v", c)
Output:

a = append(a, b...)

func Assert

func Assert(c Code) *Statement

Assert inserts a type assertion

Example
c := List(Id("b"), Id("ok")).Op(":=").Id("a").Assert(Bool())
fmt.Printf("%#v", c)
Output:

b, ok := a.(bool)

func Block

func Block(c ...Code) *Statement

Block inserts curly braces containing a statement list

Example
c := Func().Id("foo").Params().Block(Id("a").Op("=").Id("b"))
fmt.Printf("%#v", c)
Output:

func foo() {
	a = b
}
Example (Complex)
collection := func(name string, key Code, value Code) *Statement {
	if key == nil {
		// slice
		return Var().Id(name).Index().Add(value)
	} else {
		// map
		return Var().Id(name).Map(key).Add(value)
	}
}
c := Func().Id("main").Params().Block(
	collection("foo", nil, String()),
	collection("bar", String(), Int()),
)
fmt.Printf("%#v", c)
Output:

func main() {
	var foo []string
	var bar map[string]int
}

func BlockFunc

func BlockFunc(f func(*Group)) *Statement

Block inserts curly braces containing a statement list

Example
c := Func().Id("a").Params().BlockFunc(func(g *Group) {
	g.Id("a").Op("++")
	g.Id("b").Op("--")
})
fmt.Printf("%#v", c)
Output:

func a() {
	a++
	b--
}

func Bool

func Bool() *Statement

Bool inserts the bool identifier

Example
c := Var().Id("b").Bool()
fmt.Printf("%#v", c)
Output:

var b bool

func Break

func Break() *Statement

Break inserts the break keyword

Example
c := For(
	Id("i").Op(":=").Lit(0),
	Id("i").Op("<").Lit(10),
	Id("i").Op("++"),
).Block(
	If(Id("i").Op(">").Lit(5)).Block(
		Break(),
	),
)
fmt.Printf("%#v", c)
Output:

for i := 0; i < 10; i++ {
	if i > 5 {
		break
	}
}

func Byte

func Byte() *Statement

Byte inserts the byte identifier

Example
c := Id("b").Op(":=").Id("a").Assert(Byte())
fmt.Printf("%#v", c)
Output:

b := a.(byte)

func Call

func Call(c ...Code) *Statement

Call inserts parenthesis containing a comma separated list

Example
c := Qual("fmt", "Printf").Call(
	Lit("%#v: %T\n"),
	Id("a"),
	Id("b"),
)
fmt.Printf("%#v", c)
Output:

fmt.Printf("%#v: %T\n", a, b)

func CallFunc

func CallFunc(f func(*Group)) *Statement

Call inserts parenthesis containing a comma separated list

Example
f := func(name string, second string) {
	c := Id("foo").CallFunc(func(g *Group) {
		g.Id(name)
		if second != "" {
			g.Lit(second)
		}
	})
	fmt.Printf("%#v\n", c)
}
f("a", "b")
f("c", "")
Output:

foo(a, "b")
foo(c)

func Cap

func Cap(c ...Code) *Statement

Cap inserts the built in function cap

Example
c := Id("i").Op(":=").Cap(Id("v"))
fmt.Printf("%#v", c)
Output:

i := cap(v)

func Case

func Case(c ...Code) *Statement

Case inserts the case keyword, followed by a comma separated list

Example
c := Switch(Id("person")).Block(
	Case(Id("John"), Id("Peter")).CaseBlock(
		Return(Lit("male")),
	),
	Case(Id("Gill")).CaseBlock(
		Return(Lit("female")),
	),
)
fmt.Printf("%#v", c)
Output:

switch person {
case John, Peter:
	return "male"
case Gill:
	return "female"
}

func CaseBlock

func CaseBlock(c ...Code) *Statement

CaseBlock inserts a statement list preceded by a colon

Example
c := Select().Block(
	Case(Op("<-").Id("done")).CaseBlock(
		Return(Nil()),
	),
	Case(List(Err(), Id("open")).Op(":=").Op("<-").Id("fail")).CaseBlock(
		If(Op("!").Id("open")).Block(
			Return(Err()),
		),
	),
)
fmt.Printf("%#v", c)
Output:

select {
case <-done:
	return nil
case err, open := <-fail:
	if !open {
		return err
	}
}

func CaseBlockFunc

func CaseBlockFunc(f func(*Group)) *Statement

CaseBlock inserts a statement list preceded by a colon

Example
preventExitOnError := true
c := Select().Block(
	Case(Op("<-").Id("done")).CaseBlock(
		Return(Nil()),
	),
	Case(Err().Op(":=").Op("<-").Id("fail")).CaseBlockFunc(func(g *Group) {
		if !preventExitOnError {
			g.Return(Err())
		} else {
			g.Qual("fmt", "Println").Call(Err())
		}
	}),
)
fmt.Printf("%#v", c)
Output:

select {
case <-done:
	return nil
case err := <-fail:
	fmt.Println(err)
}

func CaseFunc

func CaseFunc(f func(*Group)) *Statement

Case inserts the case keyword, followed by a comma separated list

Example
samIsMale := false
c := Switch(Id("person")).Block(
	CaseFunc(func(g *Group) {
		g.Id("John")
		g.Id("Peter")
		if samIsMale {
			g.Id("Sam")
		}
	}).CaseBlock(
		Return(Lit("male")),
	),
	CaseFunc(func(g *Group) {
		g.Id("Gill")
		if !samIsMale {
			g.Id("Sam")
		}
	}).CaseBlock(
		Return(Lit("female")),
	),
)
fmt.Printf("%#v", c)
Output:

switch person {
case John, Peter:
	return "male"
case Gill, Sam:
	return "female"
}

func Chan

func Chan() *Statement

Chan inserts the chan keyword

Example
c := Func().Id("init").Params().Block(
	Id("c").Op(":=").Make(Chan().Qual("os", "Signal"), Lit(1)),
	Qual("os/signal", "Notify").Call(Id("c"), Qual("os", "Interrupt")),
	Qual("os/signal", "Notify").Call(Id("c"), Qual("syscall", "SIGTERM")),
	Go().Func().Params().Block(
		Op("<-").Id("c"),
		Id("cancel").Call(),
	).Call(),
)
fmt.Printf("%#v", c)
Output:

func init() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	go func() {
		<-c
		cancel()
	}()
}

func Close

func Close(c ...Code) *Statement

Close inserts the built in function close

func Comment

func Comment(str string) *Statement
Example
f := NewFilePath("a.b/c")
f.Func().Id("init").Params().Block(
	Qual("a.b/c", "Foo").Call().Comment("Local package - alias is omitted."),
	Qual("d.e/f", "Bar").Call().Comment("Import is automatically added."),
	Qual("g.h/f", "Baz").Call().Comment("Colliding package name is automatically renamed."),
)
fmt.Printf("%#v", f)
Output:

package c

import (
	f "d.e/f"
	f1 "g.h/f"
)

func init() {
	Foo()    // Local package - alias is omitted.
	f.Bar()  // Import is automatically added.
	f1.Baz() // Colliding package name is automatically renamed.
}
Example (Multiline)
c := Comment("a\nb")
fmt.Printf("%#v", c)
Output:

/*
a
b
*/
Example (Simple)
c := Comment("a")
fmt.Printf("%#v", c)
Output:

// a

func Commentf

func Commentf(format string, a ...interface{}) *Statement
Example
c := Commentf("a %d", 1)
fmt.Printf("%#v", c)
Output:

// a 1

func Complex

func Complex(c ...Code) *Statement

Complex inserts the built in function complex

func Complex128

func Complex128() *Statement

Complex128 inserts the complex128 identifier

func Complex64

func Complex64() *Statement

Complex64 inserts the complex64 identifier

func Const

func Const() *Statement

Const inserts the const keyword

func Continue

func Continue() *Statement

Continue inserts the continue keyword

func Copy

func Copy(c ...Code) *Statement

Copy inserts the built in function copy

func Default

func Default() *Statement

Default inserts the default keyword

func Defer

func Defer() *Statement

Defer inserts the defer keyword

func Defs

func Defs(c ...Code) *Statement

Defs inserts parenthesis containing a statement list

Example
c := Const().Defs(
	Id("a").Op("=").Lit("a"),
	Id("b").Op("=").Lit("b"),
)
fmt.Printf("%#v", c)
Output:

const (
	a = "a"
	b = "b"
)

func DefsFunc

func DefsFunc(f func(*Group)) *Statement

Defs inserts parenthesis containing a statement list

func Delete

func Delete(c ...Code) *Statement

Delete inserts the built in function delete

func Dict

func Dict(m map[Code]Code) *Statement

Dict inserts a map literal

Example
c := Id("a").Op(":=").Map(String()).String().Dict(map[Code]Code{
	Lit("a"): Lit("b"),
})
fmt.Printf("%#v", c)
Output:

a := map[string]string{
	"a": "b",
}
Example (Nil)
c := Id("a").Op(":=").Map(String()).String().Dict(nil)
fmt.Printf("%#v", c)
Output:

a := map[string]string{}

func DictFunc

func DictFunc(f func(map[Code]Code)) *Statement
Example
c := Id("a").Op(":=").Map(String()).String().DictFunc(func(m map[Code]Code) {
	m[Lit("a")] = Lit("b")
})
fmt.Printf("%#v", c)
Output:

a := map[string]string{
	"a": "b",
}

func Do

func Do(f func(*Statement)) *Statement

Do creates a new statement and calls the provided function with it as a parameter

Example
f := func(name string, isMap bool) *Statement {
	return Id(name).Op(":=").Do(func(s *Statement) {
		if isMap {
			s.Map(String()).String()
		} else {
			s.Index().String()
		}
	}).Values()
}
fmt.Printf("%#v\n%#v", f("a", true), f("b", false))
Output:

a := map[string]string{}
b := []string{}

func Else

func Else() *Statement

Else inserts the else keyword

func Empty

func Empty() *Statement

Empty token produces no output but is followed by a separator in a list.

Example
c := Id("a").Op(":=").Id("b").Index(Lit(1), Empty())
fmt.Printf("%#v", c)
Output:

a := b[1:]

func Err

func Err() *Statement

Err inserts the err identifier

Example
c := If(
	Err().Op(":=").Id("foo").Call(),
	Err().Op("!=").Nil(),
).Block(
	Return(Err()),
)
fmt.Printf("%#v", c)
Output:

if err := foo(); err != nil {
	return err
}

func Error

func Error() *Statement

Error inserts the error identifier

func Fallthrough

func Fallthrough() *Statement

Fallthrough inserts the fallthrough keyword

func False

func False() *Statement

False inserts the false identifier

func Float32

func Float32() *Statement

Float32 inserts the float32 identifier

func Float64

func Float64() *Statement

Float64 inserts the float64 identifier

func For

func For(c ...Code) *Statement

For inserts the for keyword, followed by a semicolon separated list

func ForFunc

func ForFunc(f func(*Group)) *Statement

For inserts the for keyword, followed by a semicolon separated list

func Func

func Func() *Statement

Func inserts the func keyword

Example (Declaration)
c := Func().Id("a").Params().Block()
fmt.Printf("%#v", c)
Output:

func a() {}
Example (Literal)
c := Id("a").Op(":=").Func().Params().Block()
fmt.Printf("%#v", c)
Output:

a := func() {}

func Go

func Go() *Statement

Go inserts the go keyword

func Goto

func Goto() *Statement

Goto inserts the goto keyword

func Id

func Id(name string) *Statement
Example
id := Sel(Qual("foo", "Bar"), Id("Baz"))
c := Sel(id, Id("Qux")).Call()
fmt.Printf("%#v", c)
Output:

foo.Bar.Baz.Qux()
Example (Local)
c := Id("a").Op(":=").Lit(1)
fmt.Printf("%#v", c)
Output:

a := 1
Example (Remote)
f := NewFile("main")
f.Func().Id("main").Params().Block(
	Qual("fmt", "Println").Call(
		Lit("Hello, world"),
	),
)
fmt.Printf("%#v", f)
Output:

package main

import fmt "fmt"

func main() {
	fmt.Println("Hello, world")
}
Example (Select)
c := Sel(Id("a"), Id("b"), Id("c")).Call()
fmt.Printf("%#v", c)
Output:

a.b.c()

func If

func If(c ...Code) *Statement

If inserts the if keyword, followed by a semicolon separated list

func IfFunc

func IfFunc(f func(*Group)) *Statement

If inserts the if keyword, followed by a semicolon separated list

func Imag

func Imag(c ...Code) *Statement

Imag inserts the built in function imag

func Index

func Index(c ...Code) *Statement

Index inserts square brackets containing a colon separated list

func IndexFunc

func IndexFunc(f func(*Group)) *Statement

Index inserts square brackets containing a colon separated list

func Int

func Int() *Statement

Int inserts the int identifier

func Int16

func Int16() *Statement

Int16 inserts the int16 identifier

func Int32

func Int32() *Statement

Int32 inserts the int32 identifier

func Int64

func Int64() *Statement

Int64 inserts the int64 identifier

func Int8

func Int8() *Statement

Int8 inserts the int8 identifier

func Interface

func Interface(c ...Code) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

Example
c := Type().Id("a").Interface(
	Id("b").Params().String(),
)
fmt.Printf("%#v", c)
Output:

type a interface {
	b() string
}
Example (Empty)
c := Var().Id("a").Interface()
fmt.Printf("%#v", c)
Output:

var a interface{}

func InterfaceFunc

func InterfaceFunc(f func(*Group)) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

func Iota

func Iota() *Statement

Iota inserts the iota identifier

func Len

func Len(c ...Code) *Statement

Len inserts the built in function len

func Line

func Line() *Statement

func List

func List(c ...Code) *Statement

List inserts a comma separated list

func ListFunc

func ListFunc(f func(*Group)) *Statement

List inserts a comma separated list

func Lit

func Lit(v interface{}) *Statement

func LitFunc

func LitFunc(f func() interface{}) *Statement

func Make

func Make(c ...Code) *Statement

Make inserts the built in function make

func Map

func Map(c Code) *Statement

Map inserts the map keyword, followed by square brackets

func New

func New(c ...Code) *Statement

New inserts the built in function new

func Nil

func Nil() *Statement

Nil inserts the nil identifier

func Null

func Null() *Statement

Null token produces no output but also no separator in a list.

Example (And_nil)
c := Func().Id("foo").Params(
	nil,
	Id("s").String(),
	Null(),
	Id("i").Int(),
).Block()
fmt.Printf("%#v", c)
Output:

func foo(s string, i int) {}
Example (Index)
c := Id("a").Op(":=").Id("b").Index(Lit(1), Null())
fmt.Printf("%#v", c)
Output:

a := b[1]

func Op

func Op(op string) *Statement

func Panic

func Panic(c ...Code) *Statement

Panic inserts the built in function panic

func Params

func Params(c ...Code) *Statement

Params inserts parenthesis containing a comma separated list

func ParamsFunc

func ParamsFunc(f func(*Group)) *Statement

Params inserts parenthesis containing a comma separated list

func Parens

func Parens(c Code) *Statement

Parens inserts parenthesis

Example
c := Id("a").Op("/").Parens(Id("b").Op("+").Id("c"))
fmt.Printf("%#v", c)
Output:

a / (b + c)

func Print

func Print(c ...Code) *Statement

Print inserts the built in function print

func Println

func Println(c ...Code) *Statement

Println inserts the built in function println

func Qual

func Qual(path, name string) *Statement
Example
c := Sel(Qual("a.b/c", "Foo").Call(), Id("Bar").Index(Lit(0)), Id("Baz"))
fmt.Printf("%#v", c)
Output:

c.Foo().Bar[0].Baz
Example (File)
f := NewFile("a")
f.Func().Id("main").Params().Block(
	Qual("encoding/gob", "NewEncoder").Call(),
)
fmt.Printf("%#v", f)
Output:

package a

import gob "encoding/gob"

func main() {
	gob.NewEncoder()
}
Example (Local)
f := NewFilePath("a.b/c")
f.Func().Id("main").Params().Block(
	Qual("a.b/c", "D").Call(),
)
fmt.Printf("%#v", f)
Output:

package c

func main() {
	D()
}

func Range

func Range() *Statement

Range inserts the range keyword

func Real

func Real(c ...Code) *Statement

Real inserts the built in function real

func Recover

func Recover(c ...Code) *Statement

Recover inserts the built in function recover

func Return

func Return(c ...Code) *Statement

Return inserts the return keyword, followed by a comma separated list

func ReturnFunc

func ReturnFunc(f func(*Group)) *Statement

Return inserts the return keyword, followed by a comma separated list

func Rune

func Rune() *Statement

Rune inserts the rune identifier

func Sel

func Sel(c ...Code) *Statement

Sel inserts a group of selectors - items separated by periods

func SelFunc

func SelFunc(f func(*Group)) *Statement

Sel inserts a group of selectors - items separated by periods

func Select

func Select() *Statement

Select inserts the select keyword

func String

func String() *Statement

String inserts the string identifier

func Struct

func Struct() *Statement

Struct inserts the struct keyword

func Switch

func Switch(c ...Code) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

Example
c := Switch().Id("foo").Block(
	Case().Lit("a").CaseBlock(
		Return(Lit(1)),
	),
	Case().Lit("b").CaseBlock(
		Return(Lit(2)),
	),
	Default().CaseBlock(
		Return(Lit(3)),
	),
)
fmt.Printf("%#v", c)
Output:

switch foo {
case "a":
	return 1
case "b":
	return 2
default:
	return 3
}

func SwitchFunc

func SwitchFunc(f func(*Group)) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

func Tag

func Tag(items map[string]string) *Statement

Tag adds a struct tag

Example
// Note: Tags are ordered by key when rendered
c := Type().Id("foo").Struct().Block(
	Id("A").String().Tag(map[string]string{"json": "a"}),
	Id("B").Int().Tag(map[string]string{"json": "b", "bar": "baz"}),
)
fmt.Printf("%#v", c)
Output:

type foo struct {
	A string `json:"a"`
	B int    `bar:"baz" json:"b"`
}

func True

func True() *Statement

True inserts the true identifier

func Type

func Type() *Statement

Type inserts the type keyword

func Uint

func Uint() *Statement

Uint inserts the uint identifier

func Uint16

func Uint16() *Statement

Uint16 inserts the uint16 identifier

func Uint32

func Uint32() *Statement

Uint32 inserts the uint32 identifier

func Uint64

func Uint64() *Statement

Uint64 inserts the uint64 identifier

func Uint8

func Uint8() *Statement

Uint8 inserts the uint8 identifier

func Uintptr

func Uintptr() *Statement

Uintptr inserts the uintptr identifier

func Values

func Values(c ...Code) *Statement

Values inserts curly braces containing a comma separated list

Example
c := Index().String().Values(Lit("a"), Lit("b"))
fmt.Printf("%#v", c)
Output:

[]string{"a", "b"}

func ValuesFunc

func ValuesFunc(f func(*Group)) *Statement

Values inserts curly braces containing a comma separated list

func Var

func Var() *Statement

Var inserts the var keyword

func (*Statement) Add

func (s *Statement) Add(code ...Code) *Statement

Add appends the provided code to the statement.

func (*Statement) Append

func (s *Statement) Append(c ...Code) *Statement

Append inserts the built in function append

func (*Statement) Assert

func (s *Statement) Assert(c Code) *Statement

Assert inserts a type assertion

func (*Statement) Block

func (s *Statement) Block(c ...Code) *Statement

Block inserts curly braces containing a statement list

func (*Statement) BlockFunc

func (s *Statement) BlockFunc(f func(*Group)) *Statement

Block inserts curly braces containing a statement list

func (*Statement) Bool

func (s *Statement) Bool() *Statement

Bool inserts the bool identifier

func (*Statement) Break

func (s *Statement) Break() *Statement

Break inserts the break keyword

func (*Statement) Byte

func (s *Statement) Byte() *Statement

Byte inserts the byte identifier

func (*Statement) Call

func (s *Statement) Call(c ...Code) *Statement

Call inserts parenthesis containing a comma separated list

func (*Statement) CallFunc

func (s *Statement) CallFunc(f func(*Group)) *Statement

Call inserts parenthesis containing a comma separated list

func (*Statement) Cap

func (s *Statement) Cap(c ...Code) *Statement

Cap inserts the built in function cap

func (*Statement) Case

func (s *Statement) Case(c ...Code) *Statement

Case inserts the case keyword, followed by a comma separated list

func (*Statement) CaseBlock

func (s *Statement) CaseBlock(c ...Code) *Statement

CaseBlock inserts a statement list preceded by a colon

func (*Statement) CaseBlockFunc

func (s *Statement) CaseBlockFunc(f func(*Group)) *Statement

CaseBlock inserts a statement list preceded by a colon

func (*Statement) CaseFunc

func (s *Statement) CaseFunc(f func(*Group)) *Statement

Case inserts the case keyword, followed by a comma separated list

func (*Statement) Chan

func (s *Statement) Chan() *Statement

Chan inserts the chan keyword

func (*Statement) Clone

func (s *Statement) Clone() *Statement
Example
a := Id("a")
b := Id("b")
c := Block(
	a.Call(),
	a.Call(),
	b.Clone().Call(),
	b.Clone().Call(),
)
fmt.Printf("%#v", c)
Output:

{
	a()()
	a()()
	b()
	b()
}

func (*Statement) Close

func (s *Statement) Close(c ...Code) *Statement

Close inserts the built in function close

func (*Statement) Comment

func (s *Statement) Comment(str string) *Statement

func (*Statement) Commentf

func (s *Statement) Commentf(format string, a ...interface{}) *Statement

func (*Statement) Complex

func (s *Statement) Complex(c ...Code) *Statement

Complex inserts the built in function complex

func (*Statement) Complex128

func (s *Statement) Complex128() *Statement

Complex128 inserts the complex128 identifier

func (*Statement) Complex64

func (s *Statement) Complex64() *Statement

Complex64 inserts the complex64 identifier

func (*Statement) Const

func (s *Statement) Const() *Statement

Const inserts the const keyword

func (*Statement) Continue

func (s *Statement) Continue() *Statement

Continue inserts the continue keyword

func (*Statement) Copy

func (s *Statement) Copy(c ...Code) *Statement

Copy inserts the built in function copy

func (*Statement) Default

func (s *Statement) Default() *Statement

Default inserts the default keyword

func (*Statement) Defer

func (s *Statement) Defer() *Statement

Defer inserts the defer keyword

func (*Statement) Defs

func (s *Statement) Defs(c ...Code) *Statement

Defs inserts parenthesis containing a statement list

func (*Statement) DefsFunc

func (s *Statement) DefsFunc(f func(*Group)) *Statement

Defs inserts parenthesis containing a statement list

func (*Statement) Delete

func (s *Statement) Delete(c ...Code) *Statement

Delete inserts the built in function delete

func (*Statement) Dict

func (s *Statement) Dict(m map[Code]Code) *Statement

Dict inserts a map literal

func (*Statement) DictFunc

func (s *Statement) DictFunc(f func(map[Code]Code)) *Statement

func (*Statement) Do

func (s *Statement) Do(f func(*Statement)) *Statement

Do calls the provided function with the statement as a parameter

func (*Statement) Else

func (s *Statement) Else() *Statement

Else inserts the else keyword

func (*Statement) Empty

func (s *Statement) Empty() *Statement

Empty token produces no output but is followed by a separator in a list.

func (*Statement) Err

func (s *Statement) Err() *Statement

Err inserts the err identifier

func (*Statement) Error

func (s *Statement) Error() *Statement

Error inserts the error identifier

func (*Statement) Fallthrough

func (s *Statement) Fallthrough() *Statement

Fallthrough inserts the fallthrough keyword

func (*Statement) False

func (s *Statement) False() *Statement

False inserts the false identifier

func (*Statement) Float32

func (s *Statement) Float32() *Statement

Float32 inserts the float32 identifier

func (*Statement) Float64

func (s *Statement) Float64() *Statement

Float64 inserts the float64 identifier

func (*Statement) For

func (s *Statement) For(c ...Code) *Statement

For inserts the for keyword, followed by a semicolon separated list

func (*Statement) ForFunc

func (s *Statement) ForFunc(f func(*Group)) *Statement

For inserts the for keyword, followed by a semicolon separated list

func (*Statement) Func

func (s *Statement) Func() *Statement

Func inserts the func keyword

func (*Statement) Go

func (s *Statement) Go() *Statement

Go inserts the go keyword

func (*Statement) GoString

func (s *Statement) GoString() string

func (*Statement) Goto

func (s *Statement) Goto() *Statement

Goto inserts the goto keyword

func (*Statement) Id

func (s *Statement) Id(name string) *Statement

func (*Statement) If

func (s *Statement) If(c ...Code) *Statement

If inserts the if keyword, followed by a semicolon separated list

func (*Statement) IfFunc

func (s *Statement) IfFunc(f func(*Group)) *Statement

If inserts the if keyword, followed by a semicolon separated list

func (*Statement) Imag

func (s *Statement) Imag(c ...Code) *Statement

Imag inserts the built in function imag

func (*Statement) Index

func (s *Statement) Index(c ...Code) *Statement

Index inserts square brackets containing a colon separated list

func (*Statement) IndexFunc

func (s *Statement) IndexFunc(f func(*Group)) *Statement

Index inserts square brackets containing a colon separated list

func (*Statement) Int

func (s *Statement) Int() *Statement

Int inserts the int identifier

func (*Statement) Int16

func (s *Statement) Int16() *Statement

Int16 inserts the int16 identifier

func (*Statement) Int32

func (s *Statement) Int32() *Statement

Int32 inserts the int32 identifier

func (*Statement) Int64

func (s *Statement) Int64() *Statement

Int64 inserts the int64 identifier

func (*Statement) Int8

func (s *Statement) Int8() *Statement

Int8 inserts the int8 identifier

func (*Statement) Interface

func (s *Statement) Interface(c ...Code) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

func (*Statement) InterfaceFunc

func (s *Statement) InterfaceFunc(f func(*Group)) *Statement

Interface inserts the interface keyword, followed by curly braces containing a statement list

func (*Statement) Iota

func (s *Statement) Iota() *Statement

Iota inserts the iota identifier

func (*Statement) Len

func (s *Statement) Len(c ...Code) *Statement

Len inserts the built in function len

func (*Statement) Line

func (s *Statement) Line() *Statement

func (*Statement) List

func (s *Statement) List(c ...Code) *Statement

List inserts a comma separated list

func (*Statement) ListFunc

func (s *Statement) ListFunc(f func(*Group)) *Statement

List inserts a comma separated list

func (*Statement) Lit

func (s *Statement) Lit(v interface{}) *Statement

func (*Statement) LitFunc

func (s *Statement) LitFunc(f func() interface{}) *Statement

func (*Statement) Make

func (s *Statement) Make(c ...Code) *Statement

Make inserts the built in function make

func (*Statement) Map

func (s *Statement) Map(c Code) *Statement

Map inserts the map keyword, followed by square brackets

func (*Statement) New

func (s *Statement) New(c ...Code) *Statement

New inserts the built in function new

func (*Statement) Nil

func (s *Statement) Nil() *Statement

Nil inserts the nil identifier

func (*Statement) Null

func (s *Statement) Null() *Statement

Null token produces no output but also no separator in a list.

func (*Statement) Op

func (s *Statement) Op(op string) *Statement

func (*Statement) Panic

func (s *Statement) Panic(c ...Code) *Statement

Panic inserts the built in function panic

func (*Statement) Params

func (s *Statement) Params(c ...Code) *Statement

Params inserts parenthesis containing a comma separated list

func (*Statement) ParamsFunc

func (s *Statement) ParamsFunc(f func(*Group)) *Statement

Params inserts parenthesis containing a comma separated list

func (*Statement) Parens

func (s *Statement) Parens(c Code) *Statement

Parens inserts parenthesis

func (*Statement) Print

func (s *Statement) Print(c ...Code) *Statement

Print inserts the built in function print

func (*Statement) Println

func (s *Statement) Println(c ...Code) *Statement

Println inserts the built in function println

func (*Statement) Qual

func (s *Statement) Qual(path, name string) *Statement

func (*Statement) Range

func (s *Statement) Range() *Statement

Range inserts the range keyword

func (*Statement) Real

func (s *Statement) Real(c ...Code) *Statement

Real inserts the built in function real

func (*Statement) Recover

func (s *Statement) Recover(c ...Code) *Statement

Recover inserts the built in function recover

func (*Statement) Return

func (s *Statement) Return(c ...Code) *Statement

Return inserts the return keyword, followed by a comma separated list

func (*Statement) ReturnFunc

func (s *Statement) ReturnFunc(f func(*Group)) *Statement

Return inserts the return keyword, followed by a comma separated list

func (*Statement) Rune

func (s *Statement) Rune() *Statement

Rune inserts the rune identifier

func (*Statement) Sel

func (s *Statement) Sel(c ...Code) *Statement

Sel inserts a group of selectors - items separated by periods

func (*Statement) SelFunc

func (s *Statement) SelFunc(f func(*Group)) *Statement

Sel inserts a group of selectors - items separated by periods

func (*Statement) Select

func (s *Statement) Select() *Statement

Select inserts the select keyword

func (*Statement) String

func (s *Statement) String() *Statement

String inserts the string identifier

func (*Statement) Struct

func (s *Statement) Struct() *Statement

Struct inserts the struct keyword

func (*Statement) Switch

func (s *Statement) Switch(c ...Code) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

func (*Statement) SwitchFunc

func (s *Statement) SwitchFunc(f func(*Group)) *Statement

Switch inserts the switch keyword, followed by a semicolon separated list

func (*Statement) Tag

func (s *Statement) Tag(items map[string]string) *Statement

Tag adds a struct tag

func (*Statement) True

func (s *Statement) True() *Statement

True inserts the true identifier

func (*Statement) Type

func (s *Statement) Type() *Statement

Type inserts the type keyword

func (*Statement) Uint

func (s *Statement) Uint() *Statement

Uint inserts the uint identifier

func (*Statement) Uint16

func (s *Statement) Uint16() *Statement

Uint16 inserts the uint16 identifier

func (*Statement) Uint32

func (s *Statement) Uint32() *Statement

Uint32 inserts the uint32 identifier

func (*Statement) Uint64

func (s *Statement) Uint64() *Statement

Uint64 inserts the uint64 identifier

func (*Statement) Uint8

func (s *Statement) Uint8() *Statement

Uint8 inserts the uint8 identifier

func (*Statement) Uintptr

func (s *Statement) Uintptr() *Statement

Uintptr inserts the uintptr identifier

func (*Statement) Values

func (s *Statement) Values(c ...Code) *Statement

Values inserts curly braces containing a comma separated list

func (*Statement) ValuesFunc

func (s *Statement) ValuesFunc(f func(*Group)) *Statement

Values inserts curly braces containing a comma separated list

func (*Statement) Var

func (s *Statement) Var() *Statement

Var inserts the var keyword

Jump to

Keyboard shortcuts

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