Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

建议对外开放Context的定义 #42

Open
caoyong2619 opened this issue Jun 12, 2017 · 3 comments
Open

建议对外开放Context的定义 #42

caoyong2619 opened this issue Jun 12, 2017 · 3 comments

Comments

@caoyong2619
Copy link

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

@qjebbs
Copy link

qjebbs commented Jun 12, 2017

先定义一个自定义的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
}

@lunny
Copy link
Owner

lunny commented Jun 12, 2017

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

@caoyong2619
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants