tango icon indicating copy to clipboard operation
tango copied to clipboard

建议对外开放Context的定义

Open caoyong2619 opened this issue 8 years ago • 3 comments

想在Context里注入一些自定义的东西.但是目前无法实现. 是否考虑下Context采用interface的方式,默认返回内置的Context

caoyong2619 avatar Jun 12 '17 08:06 caoyong2619

先定义一个自定义的Ctx:

type Ctx struct {
	*tango.Context
	User       *User //自定义的字段
}
//SetContext is the implement for tango interface
func (c *Ctx) SetContext(ctx *tango.Context) {
	c.Context = ctx
	c.User = ... //设置User
}

然后使用这个Ctx替换 tango.Ctx就可以了:

type Action struct {
	Ctx
}
func (a *Action) Get() interface{} {
	return a.User.Name
}

qjebbs avatar Jun 12 '17 08:06 qjebbs

对啊,有Contexter中间件和接口啊

lunny avatar Jun 12 '17 09:06 lunny

...这个方法不错,可以暂时解决我的问题. 但是这个方法必须使用struct.Get模式 无法支持func(*tango.Context)的模式

caoyong2619 avatar Jun 12 '17 09:06 caoyong2619