Documentation
¶
Index ¶
- type Code
- type File
- type Group
- func (g *Group) Add(code ...Code) *Statement
- func (g *Group) Append(c ...Code) *Statement
- func (g *Group) Assert(c Code) *Statement
- func (g *Group) Block(c ...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(c ...Code) *Statement
- func (g *Group) CallFunc(f func(*Group)) *Statement
- func (g *Group) Cap(c ...Code) *Statement
- func (g *Group) Case(c ...Code) *Statement
- func (g *Group) CaseBlock(c ...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(c ...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(c ...Code) *Statement
- func (g *Group) Default() *Statement
- func (g *Group) Defer() *Statement
- func (g *Group) Defs(c ...Code) *Statement
- func (g *Group) DefsFunc(f func(*Group)) *Statement
- func (g *Group) Delete(c ...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(c ...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(c ...Code) *Statement
- func (g *Group) IfFunc(f func(*Group)) *Statement
- func (g *Group) Imag(c ...Code) *Statement
- func (g *Group) Index(c ...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(c ...Code) *Statement
- func (g *Group) InterfaceFunc(f func(*Group)) *Statement
- func (g *Group) Iota() *Statement
- func (g *Group) Len(c ...Code) *Statement
- func (g *Group) Line() *Statement
- func (g *Group) List(c ...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(c ...Code) *Statement
- func (g *Group) Map(c Code) *Statement
- func (g *Group) New(c ...Code) *Statement
- func (g *Group) Nil() *Statement
- func (g *Group) Null() *Statement
- func (g *Group) Op(op string) *Statement
- func (g *Group) Panic(c ...Code) *Statement
- func (g *Group) Params(c ...Code) *Statement
- func (g *Group) ParamsFunc(f func(*Group)) *Statement
- func (g *Group) Parens(c Code) *Statement
- func (g *Group) Print(c ...Code) *Statement
- func (g *Group) Println(c ...Code) *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(c ...Code) *Statement
- func (g *Group) Return(c ...Code) *Statement
- func (g *Group) ReturnFunc(f func(*Group)) *Statement
- func (g *Group) Rune() *Statement
- func (g *Group) Sel(c ...Code) *Statement
- func (g *Group) SelFunc(f func(*Group)) *Statement
- func (g *Group) Select() *Statement
- func (g *Group) String() *Statement
- func (g *Group) Struct() *Statement
- func (g *Group) Switch(c ...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(c ...Code) *Statement
- func (g *Group) ValuesFunc(f func(*Group)) *Statement
- func (g *Group) Var() *Statement
- type Statement
- func Add(code ...Code) *Statement
- func Append(c ...Code) *Statement
- func Assert(c Code) *Statement
- func Block(c ...Code) *Statement
- func BlockFunc(f func(*Group)) *Statement
- func Bool() *Statement
- func Break() *Statement
- func Byte() *Statement
- func Call(c ...Code) *Statement
- func CallFunc(f func(*Group)) *Statement
- func Cap(c ...Code) *Statement
- func Case(c ...Code) *Statement
- func CaseBlock(c ...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(c ...Code) *Statement
- func Complex128() *Statement
- func Complex64() *Statement
- func Const() *Statement
- func Continue() *Statement
- func Copy(c ...Code) *Statement
- func Default() *Statement
- func Defer() *Statement
- func Defs(c ...Code) *Statement
- func DefsFunc(f func(*Group)) *Statement
- func Delete(c ...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(c ...Code) *Statement
- func ForFunc(f func(*Group)) *Statement
- func Func() *Statement
- func Go() *Statement
- func Goto() *Statement
- func Id(name string) *Statement
- func If(c ...Code) *Statement
- func IfFunc(f func(*Group)) *Statement
- func Imag(c ...Code) *Statement
- func Index(c ...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(c ...Code) *Statement
- func InterfaceFunc(f func(*Group)) *Statement
- func Iota() *Statement
- func Len(c ...Code) *Statement
- func Line() *Statement
- func List(c ...Code) *Statement
- func ListFunc(f func(*Group)) *Statement
- func Lit(v interface{}) *Statement
- func LitFunc(f func() interface{}) *Statement
- func Make(c ...Code) *Statement
- func Map(c Code) *Statement
- func New(c ...Code) *Statement
- func Nil() *Statement
- func Null() *Statement
- func Op(op string) *Statement
- func Panic(c ...Code) *Statement
- func Params(c ...Code) *Statement
- func ParamsFunc(f func(*Group)) *Statement
- func Parens(c Code) *Statement
- func Print(c ...Code) *Statement
- func Println(c ...Code) *Statement
- func Qual(path, name string) *Statement
- func Range() *Statement
- func Real(c ...Code) *Statement
- func Recover(c ...Code) *Statement
- func Return(c ...Code) *Statement
- func ReturnFunc(f func(*Group)) *Statement
- func Rune() *Statement
- func Sel(c ...Code) *Statement
- func SelFunc(f func(*Group)) *Statement
- func Select() *Statement
- func String() *Statement
- func Struct() *Statement
- func Switch(c ...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(c ...Code) *Statement
- func ValuesFunc(f func(*Group)) *Statement
- func Var() *Statement
- func (s *Statement) Add(code ...Code) *Statement
- func (s *Statement) Append(c ...Code) *Statement
- func (s *Statement) Assert(c Code) *Statement
- func (s *Statement) Block(c ...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(c ...Code) *Statement
- func (s *Statement) CallFunc(f func(*Group)) *Statement
- func (s *Statement) Cap(c ...Code) *Statement
- func (s *Statement) Case(c ...Code) *Statement
- func (s *Statement) CaseBlock(c ...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(c ...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(c ...Code) *Statement
- func (s *Statement) Default() *Statement
- func (s *Statement) Defer() *Statement
- func (s *Statement) Defs(c ...Code) *Statement
- func (s *Statement) DefsFunc(f func(*Group)) *Statement
- func (s *Statement) Delete(c ...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(c ...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(c ...Code) *Statement
- func (s *Statement) IfFunc(f func(*Group)) *Statement
- func (s *Statement) Imag(c ...Code) *Statement
- func (s *Statement) Index(c ...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(c ...Code) *Statement
- func (s *Statement) InterfaceFunc(f func(*Group)) *Statement
- func (s *Statement) Iota() *Statement
- func (s *Statement) Len(c ...Code) *Statement
- func (s *Statement) Line() *Statement
- func (s *Statement) List(c ...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(c ...Code) *Statement
- func (s *Statement) Map(c Code) *Statement
- func (s *Statement) New(c ...Code) *Statement
- func (s *Statement) Nil() *Statement
- func (s *Statement) Null() *Statement
- func (s *Statement) Op(op string) *Statement
- func (s *Statement) Panic(c ...Code) *Statement
- func (s *Statement) Params(c ...Code) *Statement
- func (s *Statement) ParamsFunc(f func(*Group)) *Statement
- func (s *Statement) Parens(c Code) *Statement
- func (s *Statement) Print(c ...Code) *Statement
- func (s *Statement) Println(c ...Code) *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(c ...Code) *Statement
- func (s *Statement) Return(c ...Code) *Statement
- func (s *Statement) ReturnFunc(f func(*Group)) *Statement
- func (s *Statement) Rune() *Statement
- func (s *Statement) Sel(c ...Code) *Statement
- func (s *Statement) SelFunc(f func(*Group)) *Statement
- func (s *Statement) Select() *Statement
- func (s *Statement) String() *Statement
- func (s *Statement) Struct() *Statement
- func (s *Statement) Switch(c ...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(c ...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 (Multiline)
- Comment (Simple)
- Commentf
- Defs
- Dict
- Dict (Nil)
- DictFunc
- Do
- Empty
- Err
- File.Anon
- File.PackageComment
- File.Render
- Func (Declaration)
- Func (Literal)
- 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
- Switch
- Tag
- Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
}
func (*Group) CaseBlockFunc ¶
CaseBlock inserts a statement list preceded by a colon
func (*Group) Complex128 ¶
Complex128 inserts the complex128 identifier
func (*Group) Do ¶
Do creates a new statement in the group and calls the provided function with it as a parameter
func (*Group) Fallthrough ¶
Fallthrough inserts the fallthrough keyword
func (*Group) Interface ¶
Interface inserts the interface keyword, followed by curly braces containing a statement list
func (*Group) InterfaceFunc ¶
Interface inserts the interface keyword, followed by curly braces containing a statement list
func (*Group) ParamsFunc ¶
Params inserts parenthesis containing a comma separated list
func (*Group) ReturnFunc ¶
Return inserts the return keyword, followed by a comma separated list
func (*Group) SwitchFunc ¶
Switch inserts the switch keyword, followed by a semicolon separated list
func (*Group) ValuesFunc ¶
Values inserts curly braces containing a comma separated list
type Statement ¶
type Statement []Code
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 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Comment ¶
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 Defs ¶
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 Dict ¶
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 ¶
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 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 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 Id ¶
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 Interface ¶
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 ¶
Interface inserts the interface keyword, followed by curly braces containing a statement list
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 ParamsFunc ¶
Params inserts parenthesis containing a comma separated list
func Parens ¶
Parens inserts parenthesis
Example ¶
c := Id("a").Op("/").Parens(Id("b").Op("+").Id("c")) fmt.Printf("%#v", c)
Output: a / (b + c)
func Qual ¶
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 ¶
Return inserts the return keyword, followed by a comma separated list
func Switch ¶
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 ¶
Switch inserts 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().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 Values ¶
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 ¶
Values inserts curly braces containing a comma separated list
func (*Statement) CaseBlockFunc ¶
CaseBlock inserts a statement list preceded by a colon
func (*Statement) Clone ¶
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) Complex128 ¶
Complex128 inserts the complex128 identifier
func (*Statement) Fallthrough ¶
Fallthrough inserts the fallthrough keyword
func (*Statement) Interface ¶
Interface inserts the interface keyword, followed by curly braces containing a statement list
func (*Statement) InterfaceFunc ¶
Interface inserts the interface keyword, followed by curly braces containing a statement list
func (*Statement) ParamsFunc ¶
Params inserts parenthesis containing a comma separated list
func (*Statement) ReturnFunc ¶
Return inserts the return keyword, followed by a comma separated list
func (*Statement) Switch ¶
Switch inserts the switch keyword, followed by a semicolon separated list
func (*Statement) SwitchFunc ¶
Switch inserts the switch keyword, followed by a semicolon separated list
func (*Statement) ValuesFunc ¶
Values inserts curly braces containing a comma separated list