gin
gin copied to clipboard
feat(route): add Engine.PathFor function
Adds the gin.Engine.PathFor function to retrieve the path of registered handlers. Resolves #3256.
any update ?
It's seem not good, it can not called in the handler. Mostly, it will result a package loop.
@iflamed, can you please post some code to reproduce the problem you're experiencing? It would be good to cover it with a unit test, if applicable.
@iflamed, can you please post some code to reproduce the problem you're experiencing? It would be good to cover it with a unit test, if applicable.
If you place Router := gin.Default() in package 'routePkg', How we use Router in a handler package handlerPkg?
Mostly, we will import handlerPkg in the routePkg, like below:
import handlerPkg
Router.GET("/path/to/go",handlerPkg.SomeHandler)
In handlerPkg, If i want create a path use Router, have to import routePkg, like below:
import routePkg
func SomeHanlder() {
Router.PathFor(getUser, ":name", "gopher")
}
@asbjornu It's not very convenient. And these code will result a package loop import.
@iflamed, right, so your issue is that Router.PathFor(getUser, …) would need to have a reference to the getUser handler, which may reside in another package? That may be resolved by providing a Router.PathForName() method, where you pass in the name of the handler as it would be returned by nameOfFunction(handler).
Would that be a good solution for you?
@iflamed, right, so your issue is that
Router.PathFor(getUser, …)would need to have a reference to thegetUserhandler, which may reside in another package? That may be resolved by providing aRouter.PathForName()method, where you pass in the name of the handler as it would be returned bynameOfFunction(handler).Would that be a good solution for you?
No, In fact, I think the most case we use PathForName will be in the handler. So the best way to create a path is use gin.Context, you should add PathForName in the Context. @asbjornu
@asbjornu Maybe you could learn something from how Laravel framework build path with router.
move to next v1.11 milestone.