对go语法感到困惑[重复]
问题描述:
This question already has an answer here:
Little bit confused with this code.
var _ QueryAppender = (*selectQuery)(nil)
I found this code in pg-go
repository and don't know why QueryAppender
declared that way. Please explain me what is the use cases when I should declare variables that way.
</div>
此问题已经存在 在这里有答案: p>
-
关键字var后的下划线和接口名称是什么意思?
\ r
2个答案
span>
li>
ul>
div>
与此代码有点混淆 。 p>
var _ QueryAppender =(* selectQuery)(nil) code> pre>
我在 pg-go 存储库,不知道为什么 QueryAppender code>就是这样声明的。 请向我解释以这种方式声明变量的用例是什么。 p> div>
答
This doesn't do anything at runtime, but unless the *selectQuery
type satisfies the interface QueryAppender
, compilation will fail. It's a kind of static assertion.