Categorygithub.com/iocgo/sdk
repositorypackage
0.0.0-20241212221350-de20e9d96df0
Repository: https://github.com/iocgo/sdk.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
该包下仅提供给iocgo工具使用的,不需要理会 Injects 的错误,在编译过程中生成.
No description provided by the author

# README

注解生成 & ioc 容器库

使用Ioc

// model.go
type A struct {

}

type B struct {
    *A
}

// @Inject(lazy="false")
func NewA() *A {
    return &A{}
}

// @Inject
func NewB(a *A) *B, error {
    return &B{a}, nil
}

// main.go
import (
    "github.com/iocgo/sdk"
)

// @Gen
func Injects(container sdk.Container) error {
   panic("auto implements")
}

func main() {
    container := sdk.NewContainer()
    err := Injects(container)
    if err != nil {
        panic(err)
    }

    err = container.Run()
    if err != nil {
        panic(err)
    }
}

// cmd/main.go
import (
    "github.com/iocgo/sdk/gen/tool"
)

func main() {
    tool.Exec()
}

使用代理

// model.go
type A struct {

}

type IEcho interface {
    Echo()
}

// @Proxy(target="IEcho")
func IEchoInvocationHandler(ctx sdk.Context[IEcho]) {
    // before
    fmt.Println(ctx.In...)

    // instance & method name
    fmt.Println(ctx.Receiver, ctx.Name)

    // do method
    ctx.Do()

    // after
    fmt.Println(ctx.Out...)
}

func (A) Echo() {
    fmt.Println("A.Echo()")
}

// @Inject(name="model.A", lazy="false", proxy="model.IEcho")
func NewA() *A {
    return &A{}
}

// main.go
import (
    "github.com/iocgo/sdk"
)

// @Gen()
func Injects(container sdk.Container) error {
   panic("auto implements")
}

func main() {
    container := sdk.NewContainer()
    err := Injects(container)
    if err != nil {
        panic(err)
    }

    err = container.Run()
    if err != nil {
        panic(err)
    }

    bean, err := sdk.InvokeAs[model.Echo](container, "model.A")
    if err != nil {
        panic(err)
    }
    bean.Echo()
}

参考示例

  1. examples
  2. sdk-examples