golang gin 使用 context 实现 ioc
golang gin 使用 context 实现 ioc gin 是一个流行的 golang webserver 的框架。 https://github.com/gin-gonic/gin gin 中 HandlerFunc (type HandlerFunc func(*Context)) 的使用随处可见, ex. Middleware , Handler 中。 1 2 3 4 router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) }) 因此,根据之前 golang context 实现 IoC 容器经验, 使用 *gin.Context 作为 IoC 容器再好不过了。 标准库 context.Context 是一个接口(interface), gin.Context 是 gin 工程自己封装的的一个 struct, 并实现了该接口。 虽然……