- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有这个辅助函数,可以正常编译:
func Middleware(adapters ...interface{}) http.HandlerFunc {
log.Info("length of adapters:", len(adapters))
if len(adapters) < 1 {
panic("Adapters need to have length > 0.");
}
h, ok := (adapters[len(adapters)-1]).(http.HandlerFunc)
if ok == false {
panic("Last argument needs to be of type http.HandlerFunc") // ERROR HERE
}
adapters = adapters[:len(adapters)-1]
for _, adapt := range adapters {
h = (adapt.(AdapterFunc))(h)
}
return h
}
我是这样调用它的:
router.HandleFunc("/share", h.makeGetMany(v)).Methods("GET")
func (h Handler) makeGetMany(v Injection) http.HandlerFunc {
return mw.Middleware(
mw.Allow("admin"),
func(w http.ResponseWriter, r *http.Request) {
log.Println("now we are sending response.");
json.NewEncoder(w).Encode(v.Share)
},
)
}
问题是我遇到了这个错误,我不知道为什么:
panic: Last argument needs to be of type http.HandlerFunc
goroutine 1 [running]:
huru/mw.Middleware(0xc420083d40, 0x2, 0x2, 0xc42011f3c0)
/home/oleg/codes/huru/api/src/huru/mw/middleware.go:301 +0x187
huru/routes/share.Handler.makeGetMany(0xc4200ae1e0, 0x10)
/home/oleg/codes/huru/api/src/huru/routes/share/share.go:62 +0x108
它确实确认适配器 slice 的长度为 2:
length of adapters:2
有人知道为什么在这种情况下该类型断言会失败吗?没有意义。也许我实际上并没有检索 slice 的最后一个参数之类的?有没有更好的方法从 slice 中弹出最后一个参数?
最佳答案
您需要使用 http.HandlerFunc()
将 mw.Middleware()
语句的第二个参数包装到 http.Handler
类型中.
func (h Handler) makeGetMany(v Injection) http.HandlerFunc {
return mw.Middleware(
mw.Allow("admin"),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("now we are sending response.");
json.NewEncoder(w).Encode(v.Share)
}),
)
}
关于go - panic : Last argument needs to be of type http. HandlerFunc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53862864/
我想知道,如果我的函数没有相似的名称,我是否需要使用命名空间? 另外我无法掌握从特定文件夹导入所有命名空间的方法... 最佳答案 I'm wondering, that if my functions
我运行 2to3 -f all -f idioms -f buffer -f set_literal -f ws_comma foo.py 输出: RefactoringTool: No change
我对 RoR 还是很陌生,我正在尝试使用 button_to delete 按钮删除一个对象。但是,使用我编写的代码,当我尝试将它获取到 destroy 方法的/needs/:id 时,它会将我带到/
当我运行代码时,我在 DEBUG Console 中发现了这个错误如下图所示错误: Restarted application in 2,804ms. [38;5;248m════════ Excep
我有一个实现 __dir__ 方法的类。但是,我并不完全确定 dir API 的一些细节。 A:__dir__ 是否真的需要返回一个列表?我的实现是使用 set 来避免两次列出属性,我需要在返回之前将
我正在尝试执行对非官方 Instagram API python 库的调用,在我修复了几个需要依赖项的错误之后,我被困在了这个错误上。 File "C:\Users\Pablo\Desktop\tx
我正在使用 SingleChildScrollView 和 Column 来显示滑动条和 gridview。 如果我在我的专栏中使用一些其他小部件,如文本、图像,应用程序显示正常。但是我的swiper
我正在尝试卸载 zsh 插件 (macos),我修改了 .zshrc 文件并从 中删除了 macos >plugin 列表,并删除路径 ~/.oh-my-zsh/plugins 中的 macos 文件
我正在尝试卸载 zsh 插件 (macos),我修改了 .zshrc 文件并从 中删除了 macos >plugin 列表,并删除路径 ~/.oh-my-zsh/plugins 中的 macos 文件
Exception caught by rendering library ═════════════════════════════════ RenderBox was not laid out:
我对 Python 很陌生。我正在试用 threading模块。我遇到了 Event对象。 事件对象有wait set clear职能。我了解等待、设置和清除正在做什么。但是我不太明白为什么会有一个单
我需要在 SQL Server 的 sql 查询中转义 [ select * from sometable where name like '[something]'; 我实际上正在寻找某个东西之前的
我的要求是这样的: 我在数据库和时区中保存以毫秒为单位的时间。例如,以毫秒为单位的时间是 1223123123232长时区是 Asia/Calcutta 。我必须将其转换为 Africa/Asmara
我的表中有两个整数列,其中第一列填充了一些随机数,第二列为空。现在是否可以将第一列按升序排序,同时按降序排序并显示为第二列?我有下表的示例。 初始表: col1 col2 5 7 3 9
我正在使用 Instruments 检查我的应用程序的内存泄漏并向我展示: NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChangeAcc
我有课 class Person { // some other fields for this object std::vector relatives; } 然后是一个返回 Person
在下面的代码中,为什么需要 .page-wrap:after? 理论上,如果没有这个,粘性页脚不应该工作吗?为什么不呢? * { margin: 0; } html, body { heigh
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我看过维基百科的图数据库,还是不明白。 什么是“无索引邻接”? 可不可以理解为“不是把主键存到其他表的行,而是直接存这些行的物理位置” 最佳答案 当您的数据可以表示为图形( map 上的路线、一些树等
我是 AngularJS 的新手,但我真的很喜欢 AngularJS 的工作方式,所以我想将它部署为我的 Google 云端点后端的客户端。然后我立即遇到两个问题: 1、放在哪里myCallback
我是一名优秀的程序员,十分优秀!