hero 子包包含一些功能,用于绑定可被handlers作为参数接受的任意对象或者函数,这些被称为依赖。

    依赖即可以是想服务这种 静态的,也可以是 动态的,例如请求的值。

    使用 Iris,你将得到真正安全的绑定。这非常快,几乎可以达到原始处理程序的性能,因为Iris试图在服务器甚至没有联机之前就计算所有内容!

    Iris提供了内置的依赖关系,可以将路由的参数与您可以立即使用的函数输入参数进行匹配。

    使用这个特性你要导入 hero 包:

    1. import (
    2. // [...]
    3. "github.com/kataras/iris/v12/hero"
    4. )

    使用 hero.Handler 函数将一个函数创建为处理器,它可以接受依赖,然后从它的输出中发送响应,像这样:

    1. func printFromTo(from, to string) string { /* [...]*/ }
    2. // [...]
    3. app.Get("/{from}/{to}", hero.Handler(printFromTo))

    正如您在上方看到的那样,Context输入参数是完全可选的。

    当软你仍然可以声明它作为第一个输入参数 - Iris足够智能, 可以轻松绑定它。

    在下面,您将看到一些旨在帮助您理解的屏幕截图:

    1. 路径参数-内建的依赖

      1. ```go

      import “github.com/kataras/iris/hero”

      helloHandler := hero.Handler(hello) app.Get(“/{to:string}”, helloHandler) // 路径参数基于 hero 函数的输入类型绑定 func hello(to string) string {

      1. return "Hello" + to

      } ```

    1. 服务-静态依赖

      1. type Service interface {
      2. SayHello(to string) string
      3. }
      4. type myTestService struct {
      5. prefix string
      6. }
      7. func (s *myTestService) SayHello(to string) string {
      8. return s.prefix + " " + to
      9. }
      10. hero.Register(&myTestService{
      11. prefix : "Service: Hello"
      12. })
      13. helloServiceHandler := hero.Handler(helloService)
      14. app.Get("/{to: string}", helloServiceHandler)
      15. // 服务使用 Register 方法绑定。
      16. // &myTestService 实现了 Service 接口,因此 hero函数可以接受一个Service。
      17. func helloService(to string, service Service) string {
      18. return service.SayHello(to)
      19. }
    2. 每个请求-动态请求

      1. ```go

      type LoginForm struct {

      1. Username string `form:"username"`
      2. Password string `form:"password"`

      }

      hero.Register(func(ctx iris.Context) form LoginForm {

      1. //
      2. ctx.ReadForm(&form)
      3. return

      })

      loginHandler := hero.Handler(login)

      app.Post(“/login”, loginHandler)

      func login(form LoginForm) string {

      1. return "Hello" + form.Username

      } ```

    另外,hero 子包增加通过函数的输出值发送响应的支持,例如:

    • 如果返回的值是 string,将会把返回值作为响应体。

    • 如果返回值是 int,将会返回值作为响应状态码。

    • 如果返回值是 error,将会返回(bad request 400),并且返回的错误作为错误原因。

    • 如果返回值是 errorint,那错误码将会是输出的值,而不是400(bad request)。

    • 如果是一个自定义的 struct,当 Content-Type 头还没有设置时,将会作为 JSON 发送。

    • 如果是一个自定义的 structstring,则第二个返回值 string 将会是 Content-Type,以此类推。

    1. func myHandler(...dependencies) string |
    2. (string, string) |
    3. (string, int) |
    4. int |
    5. (int, string) |
    6. (string, error) |
    7. error |
    8. (int, error) |
    9. (any, bool) |
    10. (customStruct, error) |
    11. customStruct |
    12. (customStruct, int) |
    13. (customStruct, string) |
    14. hero.Result |
    15. (hero.Result, error) {
    16. return a_response
    17. }

    hero.Result 是一个接口,可以使用 Dispatch(ctx iris.Context) 通过自定义的逻辑将自定义的结构体呈现出来。

    1. type Result interface {
    2. Dispatch(ctx iris.Context)
    3. }

    老实说,hero 函数理解非常简单,当你使用时,你再也不想回头了。