ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
子包 [hero](https://github.com/kataras/iris/tree/master/hero) 包含用于绑定任何对象或者方法 处理程序可以接受它们的输入参数的功能 ,这些功能称为依赖项。 对于服务等内容,依赖项可以是 `Static`;也可以是依赖于传入请求的值的 `Dynamic` 。 使用 Iris 你将获得真正安全的绑定。它非常快,几乎达到原始处理程序的性能,因为Iris试图在服务器甚至未联机之前就计算所有内容! Iris 提供内置的依赖关系,可以将路由的参数与你使用的方法的输入参数进行匹配。 要使用此功能,你应该导入hero子包: ```source-go import ( // [...] "github.com/kataras/iris/v12/hero" ) ``` 并且使用它的 `hero.Handler` 包级函数去构建处理程序,通过一个可以接受依赖关系和通过它的输出发送响应的函数,如下所示: ```source-go func printFromTo(from, to string) string { /* [...]*/ } // [...] app.Get("/{from}/{to}", hero.Handler(printFromTo)) ``` 如上所示, `iris.Context` 输入参数是完全可选的。当然, 仍然可以将其声明为  **第一个输入参数** - Iris 足够智能,可以轻松绑定它。 在下面,你将看到一些能够帮助你理解的屏幕截图: ## [1\. 路径参数-内置依赖项](https://github.com/kataras/iris/wiki/dependency-injection#1-path-parameters---built-in-dependencies) ![](https://cdn.learnku.com/uploads/images/202002/06/16294/Wj4ndML2o2.png!large) ## [2\. 服务-静态依赖性](https://github.com/kataras/iris/wiki/dependency-injection#2-services---static-dependencies) ![](https://cdn.learnku.com/uploads/images/202002/06/16294/M4j6K3oJT2.png!large) ## [3\. 每个请求-动态依赖关系](https://github.com/kataras/iris/wiki/dependency-injection#3-per-request---dynamic-dependencies) ![](https://cdn.learnku.com/uploads/images/202002/06/16294/tr3rxuwyVH.png!large) 另外, hero 子包 增加了对通过函数的 **输出值** 发送响应的支持,例如: - 如果返回值为 `string` ,则它将发送该字符串作为响应的正文。 - 如果它是一个 `int` 值,则它将作为状态代码发送。 - 如果它是一个`error` 值,则它将设置一个错误请求,并以该错误为原因。 - 如果它是一个 `error` 值和一个 `int` 值 ,则错误代码是输出整数而不是400(错误请求)。 - 如果它是一个自定义的 `struct` 值, 则当尚未设置Content-Type标头时,它将作为JSON发送。 - 如果它是一个自定义的 `struct` 值和一个 `string`值 , 则第二个输出值 string 是Content-Type,依此类推。 ```source-go func myHandler(...dependencies) string | (string, string) | (string, int) | int | (int, string) | (string, error) | error | (int, error) | (any, bool) | (customStruct, error) | customStruct | (customStruct, int) | (customStruct, string) | hero.Result | (hero.Result, error) { return a_response } ```  `hero.Result` 是一个接口, 可帮助通过自定义逻辑通过其`Dispatch(ctx iris.Context)`使用自定义逻辑呈现自定义结构。 ```source-go type Result interface { Dispatch(ctx iris.Context) } ``` 坦白地说,`hero funcs` 非常容易理解,当你开始使用它们时 **你永远不会回头**。 稍后,你将看到这些知识将如何帮助你使用 MVC 架构模式制作一个应用, Iris 为此提供了完美的 API 支持。