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 ¶ added in v0.11.4
func Invoke(fn interface{}) error
Example ¶
package main import ( "fmt" "log" "github.com/typical-go/typical-go/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/typical-go/typical-go/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 ¶ added in v0.11.4
func SetConstructors(c []*Constructor)
func SetContainer ¶ added in v0.11.4
func StartApp ¶ added in v0.11.5
StartApp start the service with gracefully stop
Example ¶
package main import ( "fmt" "log" "github.com/typical-go/typical-go/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 ¶ added in v0.9.45
type Constructor struct { Name string Fn interface{} }
Constructor details
func Constructors ¶ added in v0.11.4
func Constructors() []*Constructor
type CtorAnnot ¶ added in v0.11.4
type CtorAnnot struct{}
CtorAnnot handle @ctor annotation e.g. `@ctor (name:"NAME")`
func (*CtorAnnot) AnnotationName ¶ added in v0.11.6
func (*CtorAnnot) IsAllowed ¶ added in v0.11.6
func (a *CtorAnnot) IsAllowed(d *typgen.Annotation) bool
func (*CtorAnnot) ProcessAnnotatedFile ¶ added in v0.11.7
Click to show internal directories.
Click to hide internal directories.