Documentation ¶
Overview ¶
Package jen is a code generator for Go
Index ¶
- type Code
- type File
- type Group
- func (g *Group) Add(code ...Code) *Statement
- func (g *Group) Append(args ...Code) *Statement
- func (g *Group) AppendFunc(f func(*Group)) *Statement
- func (g *Group) Assert(typ Code) *Statement
- func (g *Group) Block(statements ...Code) *Statement
- func (g *Group) BlockFunc(f func(*Group)) *Statement
- func (g *Group) Bool() *Statement
- func (g *Group) Break() *Statement
- func (g *Group) Byte() *Statement
- func (g *Group) Call(params ...Code) *Statement
- func (g *Group) CallFunc(f func(*Group)) *Statement
- func (g *Group) Cap(v Code) *Statement
- func (g *Group) Case(cases ...Code) *Statement
- func (g *Group) CaseBlock(statements ...Code) *Statement
- func (g *Group) CaseBlockFunc(f func(*Group)) *Statement
- func (g *Group) CaseFunc(f func(*Group)) *Statement
- func (g *Group) Chan() *Statement
- func (g *Group) Close(c Code) *Statement
- func (g *Group) Comment(str string) *Statement
- func (g *Group) Commentf(format string, a ...interface{}) *Statement
- func (g *Group) Complex(r Code, i Code) *Statement
- func (g *Group) Complex128() *Statement
- func (g *Group) Complex64() *Statement
- func (g *Group) Const() *Statement
- func (g *Group) Continue() *Statement
- func (g *Group) Copy(dst Code, src Code) *Statement
- func (g *Group) Default() *Statement
- func (g *Group) Defer() *Statement
- func (g *Group) Defs(definitions ...Code) *Statement
- func (g *Group) DefsFunc(f func(*Group)) *Statement
- func (g *Group) Delete(m Code, key Code) *Statement
- func (g *Group) Dict(m map[Code]Code) *Statement
- func (g *Group) DictFunc(f func(map[Code]Code)) *Statement
- func (g *Group) Do(f func(*Statement)) *Statement
- func (g *Group) Else() *Statement
- func (g *Group) Empty() *Statement
- func (g *Group) Err() *Statement
- func (g *Group) Error() *Statement
- func (g *Group) Fallthrough() *Statement
- func (g *Group) False() *Statement
- func (g *Group) Float32() *Statement
- func (g *Group) Float64() *Statement
- func (g *Group) For(conditions ...Code) *Statement
- func (g *Group) ForFunc(f func(*Group)) *Statement
- func (g *Group) Func() *Statement
- func (g *Group) Go() *Statement
- func (g *Group) GoString() string
- func (g *Group) Goto() *Statement
- func (g *Group) Id(name string) *Statement
- func (g *Group) If(conditions ...Code) *Statement
- func (g *Group) IfFunc(f func(*Group)) *Statement
- func (g *Group) Imag(c Code) *Statement
- func (g *Group) Index(items ...Code) *Statement
- func (g *Group) IndexFunc(f func(*Group)) *Statement
- func (g *Group) Int() *Statement
- func (g *Group) Int16() *Statement
- func (g *Group) Int32() *Statement
- func (g *Group) Int64() *Statement
- func (g *Group) Int8() *Statement
- func (g *Group) Interface(methods ...Code) *Statement
- func (g *Group) InterfaceFunc(f func(*Group)) *Statement
- func (g *Group) Iota() *Statement
- func (g *Group) Len(v Code) *Statement
- func (g *Group) Line() *Statement
- func (g *Group) List(items ...Code) *Statement
- func (g *Group) ListFunc(f func(*Group)) *Statement
- func (g *Group) Lit(v interface{}) *Statement
- func (g *Group) LitFunc(f func() interface{}) *Statement
- func (g *Group) Make(args ...Code) *Statement
- func (g *Group) Map(typ Code) *Statement
- func (g *Group) New(typ Code) *Statement
- func (g *Group) Nil() *Statement
- func (g *Group) Null() *Statement
- func (g *Group) Op(op string) *Statement
- func (g *Group) Panic(v Code) *Statement
- func (g *Group) Params(params ...Code) *Statement
- func (g *Group) ParamsFunc(f func(*Group)) *Statement
- func (g *Group) Parens(item Code) *Statement
- func (g *Group) Print(args ...Code) *Statement
- func (g *Group) PrintFunc(f func(*Group)) *Statement
- func (g *Group) Println(args ...Code) *Statement
- func (g *Group) PrintlnFunc(f func(*Group)) *Statement
- func (g *Group) Qual(path, name string) *Statement
- func (g *Group) Range() *Statement
- func (g *Group) Real(c Code) *Statement
- func (g *Group) Recover() *Statement
- func (g *Group) Return(results ...Code) *Statement
- func (g *Group) ReturnFunc(f func(*Group)) *Statement
- func (g *Group) Rune() *Statement
- func (g *Group) Sel(selectors ...Code) *Statement
- func (g *Group) SelFunc(f func(*Group)) *Statement
- func (g *Group) Select() *Statement
- func (g *Group) String() *Statement
- func (g *Group) Struct(fields ...Code) *Statement
- func (g *Group) StructFunc(f func(*Group)) *Statement
- func (g *Group) Switch(conditions ...Code) *Statement
- func (g *Group) SwitchFunc(f func(*Group)) *Statement
- func (g *Group) Tag(items map[string]string) *Statement
- func (g *Group) True() *Statement
- func (g *Group) Type() *Statement
- func (g *Group) Uint() *Statement
- func (g *Group) Uint16() *Statement
- func (g *Group) Uint32() *Statement
- func (g *Group) Uint64() *Statement
- func (g *Group) Uint8() *Statement
- func (g *Group) Uintptr() *Statement
- func (g *Group) Values(values ...Code) *Statement
- func (g *Group) ValuesFunc(f func(*Group)) *Statement
- func (g *Group) Var() *Statement
- type Statement
- func Add(code ...Code) *Statement
- func Append(args ...Code) *Statement
- func AppendFunc(f func(*Group)) *Statement
- func Assert(typ Code) *Statement
- func Block(statements ...Code) *Statement
- func BlockFunc(f func(*Group)) *Statement
- func Bool() *Statement
- func Break() *Statement
- func Byte() *Statement
- func Call(params ...Code) *Statement
- func CallFunc(f func(*Group)) *Statement
- func Cap(v Code) *Statement
- func Case(cases ...Code) *Statement
- func CaseBlock(statements ...Code) *Statement
- func CaseBlockFunc(f func(*Group)) *Statement
- func CaseFunc(f func(*Group)) *Statement
- func Chan() *Statement
- func Close(c Code) *Statement
- func Comment(str string) *Statement
- func Commentf(format string, a ...interface{}) *Statement
- func Complex(r Code, i Code) *Statement
- func Complex128() *Statement
- func Complex64() *Statement
- func Const() *Statement
- func Continue() *Statement
- func Copy(dst Code, src Code) *Statement
- func Default() *Statement
- func Defer() *Statement
- func Defs(definitions ...Code) *Statement
- func DefsFunc(f func(*Group)) *Statement
- func Delete(m Code, key Code) *Statement
- func Dict(m map[Code]Code) *Statement
- func DictFunc(f func(map[Code]Code)) *Statement
- func Do(f func(*Statement)) *Statement
- func Else() *Statement
- func Empty() *Statement
- func Err() *Statement
- func Error() *Statement
- func Fallthrough() *Statement
- func False() *Statement
- func Float32() *Statement
- func Float64() *Statement
- func For(conditions ...Code) *Statement
- func ForFunc(f func(*Group)) *Statement
- func Func() *Statement
- func Go() *Statement
- func Goto() *Statement
- func Id(name string) *Statement
- func If(conditions ...Code) *Statement
- func IfFunc(f func(*Group)) *Statement
- func Imag(c Code) *Statement
- func Index(items ...Code) *Statement
- func IndexFunc(f func(*Group)) *Statement
- func Int() *Statement
- func Int16() *Statement
- func Int32() *Statement
- func Int64() *Statement
- func Int8() *Statement
- func Interface(methods ...Code) *Statement
- func InterfaceFunc(f func(*Group)) *Statement
- func Iota() *Statement
- func Len(v Code) *Statement
- func Line() *Statement
- func List(items ...Code) *Statement
- func ListFunc(f func(*Group)) *Statement
- func Lit(v interface{}) *Statement
- func LitFunc(f func() interface{}) *Statement
- func Make(args ...Code) *Statement
- func Map(typ Code) *Statement
- func New(typ Code) *Statement
- func Nil() *Statement
- func Null() *Statement
- func Op(op string) *Statement
- func Panic(v Code) *Statement
- func Params(params ...Code) *Statement
- func ParamsFunc(f func(*Group)) *Statement
- func Parens(item Code) *Statement
- func Print(args ...Code) *Statement
- func PrintFunc(f func(*Group)) *Statement
- func Println(args ...Code) *Statement
- func PrintlnFunc(f func(*Group)) *Statement
- func Qual(path, name string) *Statement
- func Range() *Statement
- func Real(c Code) *Statement
- func Recover() *Statement
- func Return(results ...Code) *Statement
- func ReturnFunc(f func(*Group)) *Statement
- func Rune() *Statement
- func Sel(selectors ...Code) *Statement
- func SelFunc(f func(*Group)) *Statement
- func Select() *Statement
- func String() *Statement
- func Struct(fields ...Code) *Statement
- func StructFunc(f func(*Group)) *Statement
- func Switch(conditions ...Code) *Statement
- func SwitchFunc(f func(*Group)) *Statement
- func Tag(items map[string]string) *Statement
- func True() *Statement
- func Type() *Statement
- func Uint() *Statement
- func Uint16() *Statement
- func Uint32() *Statement
- func Uint64() *Statement
- func Uint8() *Statement
- func Uintptr() *Statement
- func Values(values ...Code) *Statement
- func ValuesFunc(f func(*Group)) *Statement
- func Var() *Statement
- func (s *Statement) Add(code ...Code) *Statement
- func (s *Statement) Append(args ...Code) *Statement
- func (s *Statement) AppendFunc(f func(*Group)) *Statement
- func (s *Statement) Assert(typ Code) *Statement
- func (s *Statement) Block(statements ...Code) *Statement
- func (s *Statement) BlockFunc(f func(*Group)) *Statement
- func (s *Statement) Bool() *Statement
- func (s *Statement) Break() *Statement
- func (s *Statement) Byte() *Statement
- func (s *Statement) Call(params ...Code) *Statement
- func (s *Statement) CallFunc(f func(*Group)) *Statement
- func (s *Statement) Cap(v Code) *Statement
- func (s *Statement) Case(cases ...Code) *Statement
- func (s *Statement) CaseBlock(statements ...Code) *Statement
- func (s *Statement) CaseBlockFunc(f func(*Group)) *Statement
- func (s *Statement) CaseFunc(f func(*Group)) *Statement
- func (s *Statement) Chan() *Statement
- func (s *Statement) Clone() *Statement
- func (s *Statement) Close(c Code) *Statement
- func (s *Statement) Comment(str string) *Statement
- func (s *Statement) Commentf(format string, a ...interface{}) *Statement
- func (s *Statement) Complex(r Code, i Code) *Statement
- func (s *Statement) Complex128() *Statement
- func (s *Statement) Complex64() *Statement
- func (s *Statement) Const() *Statement
- func (s *Statement) Continue() *Statement
- func (s *Statement) Copy(dst Code, src Code) *Statement
- func (s *Statement) Default() *Statement
- func (s *Statement) Defer() *Statement
- func (s *Statement) Defs(definitions ...Code) *Statement
- func (s *Statement) DefsFunc(f func(*Group)) *Statement
- func (s *Statement) Delete(m Code, key Code) *Statement
- func (s *Statement) Dict(m map[Code]Code) *Statement
- func (s *Statement) DictFunc(f func(map[Code]Code)) *Statement
- func (s *Statement) Do(f func(*Statement)) *Statement
- func (s *Statement) Else() *Statement
- func (s *Statement) Empty() *Statement
- func (s *Statement) Err() *Statement
- func (s *Statement) Error() *Statement
- func (s *Statement) Fallthrough() *Statement
- func (s *Statement) False() *Statement
- func (s *Statement) Float32() *Statement
- func (s *Statement) Float64() *Statement
- func (s *Statement) For(conditions ...Code) *Statement
- func (s *Statement) ForFunc(f func(*Group)) *Statement
- func (s *Statement) Func() *Statement
- func (s *Statement) Go() *Statement
- func (s *Statement) GoString() string
- func (s *Statement) Goto() *Statement
- func (s *Statement) Id(name string) *Statement
- func (s *Statement) If(conditions ...Code) *Statement
- func (s *Statement) IfFunc(f func(*Group)) *Statement
- func (s *Statement) Imag(c Code) *Statement
- func (s *Statement) Index(items ...Code) *Statement
- func (s *Statement) IndexFunc(f func(*Group)) *Statement
- func (s *Statement) Int() *Statement
- func (s *Statement) Int16() *Statement
- func (s *Statement) Int32() *Statement
- func (s *Statement) Int64() *Statement
- func (s *Statement) Int8() *Statement
- func (s *Statement) Interface(methods ...Code) *Statement
- func (s *Statement) InterfaceFunc(f func(*Group)) *Statement
- func (s *Statement) Iota() *Statement
- func (s *Statement) Len(v Code) *Statement
- func (s *Statement) Line() *Statement
- func (s *Statement) List(items ...Code) *Statement
- func (s *Statement) ListFunc(f func(*Group)) *Statement
- func (s *Statement) Lit(v interface{}) *Statement
- func (s *Statement) LitFunc(f func() interface{}) *Statement
- func (s *Statement) Make(args ...Code) *Statement
- func (s *Statement) Map(typ Code) *Statement
- func (s *Statement) New(typ Code) *Statement
- func (s *Statement) Nil() *Statement
- func (s *Statement) Null() *Statement
- func (s *Statement) Op(op string) *Statement
- func (s *Statement) Panic(v Code) *Statement
- func (s *Statement) Params(params ...Code) *Statement
- func (s *Statement) ParamsFunc(f func(*Group)) *Statement
- func (s *Statement) Parens(item Code) *Statement
- func (s *Statement) Print(args ...Code) *Statement
- func (s *Statement) PrintFunc(f func(*Group)) *Statement
- func (s *Statement) Println(args ...Code) *Statement
- func (s *Statement) PrintlnFunc(f func(*Group)) *Statement
- func (s *Statement) Qual(path, name string) *Statement
- func (s *Statement) Range() *Statement
- func (s *Statement) Real(c Code) *Statement
- func (s *Statement) Recover() *Statement
- func (s *Statement) Return(results ...Code) *Statement
- func (s *Statement) ReturnFunc(f func(*Group)) *Statement
- func (s *Statement) Rune() *Statement
- func (s *Statement) Sel(selectors ...Code) *Statement
- func (s *Statement) SelFunc(f func(*Group)) *Statement
- func (s *Statement) Select() *Statement
- func (s *Statement) String() *Statement
- func (s *Statement) Struct(fields ...Code) *Statement
- func (s *Statement) StructFunc(f func(*Group)) *Statement
- func (s *Statement) Switch(conditions ...Code) *Statement
- func (s *Statement) SwitchFunc(f func(*Group)) *Statement
- func (s *Statement) Tag(items map[string]string) *Statement
- func (s *Statement) True() *Statement
- func (s *Statement) Type() *Statement
- func (s *Statement) Uint() *Statement
- func (s *Statement) Uint16() *Statement
- func (s *Statement) Uint32() *Statement
- func (s *Statement) Uint64() *Statement
- func (s *Statement) Uint8() *Statement
- func (s *Statement) Uintptr() *Statement
- func (s *Statement) Values(values ...Code) *Statement
- func (s *Statement) ValuesFunc(f func(*Group)) *Statement
- func (s *Statement) Var() *Statement
Examples ¶
- Add
- Append
- Append (More)
- Assert
- Block
- Block (Complex)
- BlockFunc
- Bool
- Break
- Byte
- Call
- CallFunc
- Cap
- Case
- CaseBlock
- CaseBlockFunc
- CaseFunc
- Chan
- Comment
- Comment (Formatting_disabled)
- Comment (Multiline)
- Comment (Simple)
- Commentf
- Defer
- Defs
- Dict
- Dict (Nil)
- DictFunc
- Do
- Empty
- Err
- File.Anon
- File.PackageComment
- File.Render
- Func (Declaration)
- Func (Literal)
- Goto
- Id
- Id (Local)
- Id (Remote)
- Id (Select)
- Interface
- Interface (Empty)
- NewFile
- NewFilePath
- NewFilePathName
- Null (And_nil)
- Null (Index)
- Parens
- Qual
- Qual (File)
- Qual (Local)
- Statement.Clone
- Struct
- Struct (Empty)
- Switch
- Tag
- Values
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
}
Code represents an item of code that can be rendered.
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 }
File represents a single source file. Package imports are managed automaticaly by File.
func NewFile ¶
NewFile Creates a new file, with the specified package name.
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 ¶
NewFilePath creates a new file while specifying the package path - the package name is inferred from the path.
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 ¶
NewFilePathName creates a new file with the specified package path and name.
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 ¶
Anon adds an anonymous import:
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) PackageComment ¶
PackageComment adds a comment to the top of the file, above the package keyword:
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() {}
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a list of Code items, separated by tokens with an optional open and close token.
func (*Group) AppendFunc ¶ added in v0.9.1
AppendFunc renders the append built-in function.
func (*Group) Assert ¶
Assert renders a period followed by a single item enclosed by parenthesis. Use for type assertions.
func (*Group) Block ¶
Block renders a statement list enclosed by curly braces. Use for all code blocks.
func (*Group) BlockFunc ¶
BlockFunc renders a statement list enclosed by curly braces. Use for all code blocks.
func (*Group) Call ¶
Call renders a comma separated list enclosed by parenthesis. Use for function calls.
func (*Group) CallFunc ¶
CallFunc renders a comma separated list enclosed by parenthesis. Use for function calls.
func (*Group) CaseBlock ¶
CaseBlock renders a statement list preceded by a colon. Use to build switch / select statements.
func (*Group) CaseBlockFunc ¶
CaseBlockFunc renders a statement list preceded by a colon. Use to build switch / select statements.
func (*Group) Comment ¶
Comment adds a comment. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
func (*Group) Commentf ¶
Commentf adds a comment, using a format string and a list of parameters. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
func (*Group) Complex128 ¶
Complex128 renders the complex128 identifier.
func (*Group) Defs ¶
Defs renders a list of statements enclosed in parenthesis. Use for definition lists.
func (*Group) DefsFunc ¶
DefsFunc renders a list of statements enclosed in parenthesis. Use for definition lists.
func (*Group) Dict ¶
Dict takes a map[Code]Code and renders a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
func (*Group) DictFunc ¶
DictFunc executes a func(map[Code]Code). The value of the parameter is then rendered as a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
func (*Group) Do ¶
Do creates a new statement in the group and calls the provided function with it as a parameter
func (*Group) Empty ¶
Empty adds an empty item. Empty items render nothing but are followed by a separator in lists.
func (*Group) Fallthrough ¶
Fallthrough renders the fallthrough keyword.
func (*Group) Index ¶
Index renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func (*Group) IndexFunc ¶
IndexFunc renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func (*Group) Interface ¶
Interface renders the interface keyword followed by curly braces containing a method list.
func (*Group) InterfaceFunc ¶
InterfaceFunc renders the interface keyword followed by curly braces containing a method list.
func (*Group) List ¶
List renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func (*Group) ListFunc ¶
ListFunc renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func (*Group) LitFunc ¶
LitFunc renders a literal, using the format provided by the fmt package %#v verb. The value to render is generated by executing the provided function.
func (*Group) Map ¶
Map renders the map keyword followed by a single item enclosed by square brackets. Use for map definitions.
func (*Group) Null ¶
Null adds a null item. Null items render nothing and are not followed by a separator in lists.
func (*Group) Params ¶
Params renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func (*Group) ParamsFunc ¶
ParamsFunc renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func (*Group) Parens ¶
Parens renders a single item in parenthesis. Use for type conversion or to specify evaluation order.
func (*Group) PrintlnFunc ¶ added in v0.9.1
PrintlnFunc renders the println built-in function.
func (*Group) Qual ¶
Qual renders a qualified identifier. Imports are automatically added when used with a File. If the path matches the local path, the package name is omitted. If package names conflict they are automatically renamed.
func (*Group) ReturnFunc ¶
ReturnFunc renders the return keyword followed by a comma separated list.
func (*Group) Struct ¶
Struct renders the struct keyword followed by curly braces containing a field list.
func (*Group) StructFunc ¶ added in v0.9.3
StructFunc renders the struct keyword followed by curly braces containing a field list.
func (*Group) SwitchFunc ¶
SwitchFunc renders the switch keyword followed by a semicolon separated list.
func (*Group) Values ¶
Values renders a comma separated list enclosed by curly braces. Use for slice literals.
func (*Group) ValuesFunc ¶
ValuesFunc renders a comma separated list enclosed by curly braces. Use for slice literals.
type Statement ¶
type Statement []Code
Statement represents a simple list of code items. When rendered the items are separated by spaces.
func Add ¶
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 ¶
Append renders the append built-in function.
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 AppendFunc ¶ added in v0.9.1
AppendFunc renders the append built-in function.
func Assert ¶
Assert renders a period followed by a single item enclosed by parenthesis. Use for type assertions.
Example ¶
c := List(Id("b"), Id("ok")).Op(":=").Id("a").Assert(Bool()) fmt.Printf("%#v", c)
Output: b, ok := a.(bool)
func Block ¶
Block renders a statement list enclosed by curly braces. Use for all code blocks.
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 ¶
BlockFunc renders a statement list enclosed by curly braces. Use for all code blocks.
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 renders the bool identifier.
Example ¶
c := Var().Id("b").Bool() fmt.Printf("%#v", c)
Output: var b bool
func Break ¶
func Break() *Statement
Break renders 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 renders the byte identifier.
Example ¶
c := Id("b").Op(":=").Id("a").Assert(Byte()) fmt.Printf("%#v", c)
Output: b := a.(byte)
func Call ¶
Call renders a comma separated list enclosed by parenthesis. Use for function calls.
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 ¶
CallFunc renders a comma separated list enclosed by parenthesis. Use for function calls.
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 ¶
Cap renders the cap built-in function.
Example ¶
c := Id("i").Op(":=").Cap(Id("v")) fmt.Printf("%#v", c)
Output: i := cap(v)
func Case ¶
Case renders 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 ¶
CaseBlock renders a statement list preceded by a colon. Use to build switch / select statements.
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 ¶
CaseBlockFunc renders a statement list preceded by a colon. Use to build switch / select statements.
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 ¶
CaseFunc renders 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 renders 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 Comment ¶
Comment adds a comment. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
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 (Formatting_disabled) ¶
c := Id("foo").Call(Comment("/* inline */")).Comment("//close") fmt.Printf("%#v", c)
Output: foo( /* inline */ ) //close
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 ¶
Commentf adds a comment, using a format string and a list of parameters. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
Example ¶
c := Commentf("a %d", 1) fmt.Printf("%#v", c)
Output: // a 1
func Defer ¶
func Defer() *Statement
Defer renders the defer keyword.
Example ¶
c := Defer().Id("foo").Call() fmt.Printf("%#v", c)
Output: defer foo()
func Defs ¶
Defs renders a list of statements enclosed in parenthesis. Use for definition lists.
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 ¶
DefsFunc renders a list of statements enclosed in parenthesis. Use for definition lists.
func Dict ¶
Dict takes a map[Code]Code and renders a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
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 ¶
DictFunc executes a func(map[Code]Code). The value of the parameter is then rendered as a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
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 ¶
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 Empty ¶
func Empty() *Statement
Empty adds an empty item. Empty items render nothing but are followed by a separator in lists.
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 renders 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 Func ¶
func Func() *Statement
Func renders 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 Goto ¶
func Goto() *Statement
Goto renders the goto keyword.
Example ¶
c := Goto().Id("Outer") fmt.Printf("%#v", c)
Output: goto Outer
func Id ¶
Id renders an identifier.
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 Index ¶
Index renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func IndexFunc ¶
IndexFunc renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func Interface ¶
Interface renders the interface keyword followed by curly braces containing a method 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 ¶
InterfaceFunc renders the interface keyword followed by curly braces containing a method list.
func List ¶
List renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func ListFunc ¶
ListFunc renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func Lit ¶
func Lit(v interface{}) *Statement
Lit renders a literal, using the format provided by the fmt package %#v verb.
func LitFunc ¶
func LitFunc(f func() interface{}) *Statement
LitFunc renders a literal, using the format provided by the fmt package %#v verb. The value to render is generated by executing the provided function.
func Map ¶
Map renders the map keyword followed by a single item enclosed by square brackets. Use for map definitions.
func Null ¶
func Null() *Statement
Null adds a null item. Null items render nothing and are not followed by a separator in lists.
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 Params ¶
Params renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func ParamsFunc ¶
ParamsFunc renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func Parens ¶
Parens renders a single item in parenthesis. Use for type conversion or to specify evaluation order.
Example ¶
c := Id("a").Op("/").Parens(Id("b").Op("+").Id("c")) fmt.Printf("%#v", c)
Output: a / (b + c)
func PrintlnFunc ¶ added in v0.9.1
PrintlnFunc renders the println built-in function.
func Qual ¶
Qual renders a qualified identifier. Imports are automatically added when used with a File. If the path matches the local path, the package name is omitted. If package names conflict they are automatically renamed.
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 ReturnFunc ¶
ReturnFunc renders the return keyword followed by a comma separated list.
func Struct ¶
Struct renders the struct keyword followed by curly braces containing a field list.
Example ¶
c := Type().Id("foo").Struct( List(Id("x"), Id("y")).Int(), Id("u").Float32(), Id("_").Float32().Comment("padding"), Id("A").Op("*").Index().Int(), Id("F").Func().Params(), ) fmt.Printf("%#v", c)
Output: type foo struct { x, y int u float32 _ float32 // padding A *[]int F func() }
Example (Empty) ¶
c := Id("c").Op(":=").Make(Chan().Struct()) fmt.Printf("%#v", c)
Output: c := make(chan struct{})
func StructFunc ¶ added in v0.9.3
StructFunc renders the struct keyword followed by curly braces containing a field list.
func Switch ¶
Switch renders 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 ¶
SwitchFunc renders the switch keyword followed by a semicolon separated list.
func Tag ¶
Tag adds a struct tag
Example ¶
// Note: Tags are ordered by key when rendered c := Type().Id("foo").Struct( 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 Values ¶
Values renders a comma separated list enclosed by curly braces. Use for slice literals.
Example ¶
c := Index().String().Values(Lit("a"), Lit("b")) fmt.Printf("%#v", c)
Output: []string{"a", "b"}
func ValuesFunc ¶
ValuesFunc renders a comma separated list enclosed by curly braces. Use for slice literals.
func (*Statement) AppendFunc ¶ added in v0.9.1
AppendFunc renders the append built-in function.
func (*Statement) Assert ¶
Assert renders a period followed by a single item enclosed by parenthesis. Use for type assertions.
func (*Statement) Block ¶
Block renders a statement list enclosed by curly braces. Use for all code blocks.
func (*Statement) BlockFunc ¶
BlockFunc renders a statement list enclosed by curly braces. Use for all code blocks.
func (*Statement) Call ¶
Call renders a comma separated list enclosed by parenthesis. Use for function calls.
func (*Statement) CallFunc ¶
CallFunc renders a comma separated list enclosed by parenthesis. Use for function calls.
func (*Statement) CaseBlock ¶
CaseBlock renders a statement list preceded by a colon. Use to build switch / select statements.
func (*Statement) CaseBlockFunc ¶
CaseBlockFunc renders a statement list preceded by a colon. Use to build switch / select statements.
func (*Statement) Clone ¶
Clone makes a copy of the Statement, so further tokens can be appended without affecting the original.
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) Comment ¶
Comment adds a comment. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
func (*Statement) Commentf ¶
Commentf adds a comment, using a format string and a list of parameters. If the provided string contains a newline, the comment is formatted in multiline style. If the comment string starts with "//" or "/*", the automatic formatting is disabled and the string is rendered directly.
func (*Statement) Complex128 ¶
Complex128 renders the complex128 identifier.
func (*Statement) Defs ¶
Defs renders a list of statements enclosed in parenthesis. Use for definition lists.
func (*Statement) DefsFunc ¶
DefsFunc renders a list of statements enclosed in parenthesis. Use for definition lists.
func (*Statement) Dict ¶
Dict takes a map[Code]Code and renders a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
func (*Statement) DictFunc ¶
DictFunc executes a func(map[Code]Code). The value of the parameter is then rendered as a list of colon separated key value pairs, enclosed in curly braces. Use for map literals.
func (*Statement) Empty ¶
Empty adds an empty item. Empty items render nothing but are followed by a separator in lists.
func (*Statement) Fallthrough ¶
Fallthrough renders the fallthrough keyword.
func (*Statement) GoString ¶
GoString renders the Statement for testing. Any error will cause a panic.
func (*Statement) Index ¶
Index renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func (*Statement) IndexFunc ¶
IndexFunc renders a colon separated list enclosed by square brackets. Use for array / slice indexes and definitions.
func (*Statement) Interface ¶
Interface renders the interface keyword followed by curly braces containing a method list.
func (*Statement) InterfaceFunc ¶
InterfaceFunc renders the interface keyword followed by curly braces containing a method list.
func (*Statement) List ¶
List renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func (*Statement) ListFunc ¶
ListFunc renders a comma separated list with no open or closing tokens. Use for multiple return functions.
func (*Statement) Lit ¶
Lit renders a literal, using the format provided by the fmt package %#v verb.
func (*Statement) LitFunc ¶
LitFunc renders a literal, using the format provided by the fmt package %#v verb. The value to render is generated by executing the provided function.
func (*Statement) Map ¶
Map renders the map keyword followed by a single item enclosed by square brackets. Use for map definitions.
func (*Statement) Null ¶
Null adds a null item. Null items render nothing and are not followed by a separator in lists.
func (*Statement) Params ¶
Params renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func (*Statement) ParamsFunc ¶
ParamsFunc renders a comma separated list enclosed by parenthesis. Use for function parameters and method receivers.
func (*Statement) Parens ¶
Parens renders a single item in parenthesis. Use for type conversion or to specify evaluation order.
func (*Statement) PrintlnFunc ¶ added in v0.9.1
PrintlnFunc renders the println built-in function.
func (*Statement) Qual ¶
Qual renders a qualified identifier. Imports are automatically added when used with a File. If the path matches the local path, the package name is omitted. If package names conflict they are automatically renamed.
func (*Statement) ReturnFunc ¶
ReturnFunc renders the return keyword followed by a comma separated list.
func (*Statement) Struct ¶
Struct renders the struct keyword followed by curly braces containing a field list.
func (*Statement) StructFunc ¶ added in v0.9.3
StructFunc renders the struct keyword followed by curly braces containing a field list.
func (*Statement) Switch ¶
Switch renders the switch keyword followed by a semicolon separated list.
func (*Statement) SwitchFunc ¶
SwitchFunc renders the switch keyword followed by a semicolon separated list.
func (*Statement) Values ¶
Values renders a comma separated list enclosed by curly braces. Use for slice literals.
func (*Statement) ValuesFunc ¶
ValuesFunc renders a comma separated list enclosed by curly braces. Use for slice literals.