英文:
Extreme noob query about interface
问题
关于这段代码的解释如下:
(*http.ResponseWriter)(nil) 是类型断言的一种形式。它将 nil 转换为 http.ResponseWriter 类型的指针。
接口可以包含指针类型。在 Go 语言中,接口是一种抽象类型,可以用于表示其他类型的集合。这意味着接口可以接受指向实现该接口的类型的指针。
希望对你有帮助!
英文:
With reference to this code
How to interpret this code?
<code> (*http.ResponseWriter)(nil) </code>
Is it type assertion or something else? I dont understand.
Also can interfaces have pointers?
Thanks
答案1
得分: 1
这是一个nil指针。
nil指针有一个类型,在这种情况下是*http.ResponseWriter。
编辑以回答评论部分的问题:
这样做的原因是inject(Martini的依赖注入器)通过MapTo将接口类型映射到该接口的实现。
由于它只对接口的_类型_(作为第二个参数)感兴趣,所以一个nil指针就足够了。
英文:
It's a nil pointer.
nil pointers have a type, in this case it's *http.ResponseWriter.
Edit to answer question in comment section:
The reason for doing this is that inject (Martini's dependency injector) maps an interface type to an implementation of that interface through MapTo.
As it's really only interested in the interface's type (as second argument), a nil pointer is enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论