2015-08-26 45 views
0

我在我的Go项目中使用https://github.com/julienschmidt/httprouterhttprouter设置自定义NotFound

我问了一会儿这个问题,这是由@icza解决:httprouter configuring NotFound但现在,开始一个新的项目,并使用非常类似的代码,我似乎在控制台中出现错误。

尝试配置NotFoundMethodNotAllowed我使用自定义处理程序:

router.NotFound = customNotFound 
router.MethodNotAllowed = customMethodNotAllowed 

生产:

cannot use customNotFound (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:                    
     func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)                         

cannot use customMethodNotAllowed (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:                  
     func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method) 

我的功能是这样的:

​​

以前有一些在过去的几个星期内打破了这个软件包的变化因为我无法弄清楚为什么我在一个项目中发现错误而不是在另一个项目中发生错误?

回答

0

由于httprouter中提交70708e4600,router.NotFound不再是http.HandlerFunc而是http.Handler。因此,如果您使用最近提交的httprouter,则必须通过http://golang.org/pkg/net/http/#HandlerFunc来修改您的功能。

以下应工作(未经测试):

router.NotFound = http.HandlerFunc(customNotFound)