Documentation ¶
Index ¶
- Variables
- func Container() (*dig.Container, error)
- func Invoke(fn interface{}) error
- func Provide(name string, fn interface{})
- func Reset()
- func SetConstructors(c []*Constructor)
- func SetContainer(c *dig.Container)
- func StartApp(startFn, stopFn interface{}, exitSigs ...os.Signal) error
- type Constructor
- type CtorAnnot
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
DefaultCtorAnnot = "@ctor"
)
Functions ¶
func Invoke ¶
func Invoke(fn interface{}) error
Example ¶
package main import ( "fmt" "log" "github.com/kmrtftech/tg-framework/pkg/typapp" ) func main() { typapp.Reset() typapp.Provide("", func() string { return "world" }) err := typapp.Invoke(func(text string) { fmt.Printf("hello %s\n", text) }) if err != nil { log.Fatal(err) } }
Output: hello world
Example (Second) ¶
package main import ( "fmt" "log" "github.com/kmrtftech/tg-framework/pkg/typapp" "go.uber.org/dig" ) func main() { typapp.Reset() typapp.Provide("t1", func() string { return "hello" }) // provide same type typapp.Provide("t2", func() string { return "world" }) // provide same type type param struct { dig.In Text1 string `name:"t1"` Text2 string `name:"t2"` } printHello := func(p param) { fmt.Printf("%s %s\n", p.Text1, p.Text2) } if err := typapp.Invoke(printHello); err != nil { log.Fatal(err) } }
Output: hello world
func SetConstructors ¶
func SetConstructors(c []*Constructor)
func SetContainer ¶
func StartApp ¶
StartApp start the service with gracefully stop
Example ¶
package main import ( "fmt" "log" "github.com/kmrtftech/tg-framework/pkg/typapp" ) func main() { typapp.Reset() // make sure constructor and container is empty (optional) typapp.Provide("", func() string { return "world" }) startFn := func(text string) { fmt.Printf("hello %s\n", text) } stopFn := func() { fmt.Println("bye2") } if err := typapp.StartApp(startFn, stopFn); err != nil { log.Fatal(err) } }
Output: hello world bye2
Types ¶
type Constructor ¶
type Constructor struct { Name string Fn interface{} }
Constructor details
func Constructors ¶
func Constructors() []*Constructor
type CtorAnnot ¶
type CtorAnnot struct{}
CtorAnnot handle @ctor annotation e.g. `@ctor (name:"NAME")`
func (*CtorAnnot) AnnotationName ¶
func (*CtorAnnot) ProcessAnnotatedFile ¶
Click to show internal directories.
Click to hide internal directories.