在 Gin 源码中 有一行代码如下

var _ IRouter = &RouterGroup{}

乍一看, 是一个 赋值 操作, 但是前面又使用了 空白描述符(下划线) 。这是什么意思呢?

答案是:接口类型检查

在 《Effective GO》 Interface Check 中的描述有相关描述。全文如下。

json.RawMessagejson.Marshaler
var _ json.Marshaler = (*RawMessage)(nil)

*RawMessageMarshaler*RawMessageMarshalerjson.Marshaler

The appearance of the blank identifier in this construct indicates that the declaration exists only for the type checking, not to create a variable. Don’t do this for every type that satisfies an interface, though. By convention, such declarations are only used when there are no static conversions already present in the code, which is a rare event.

简单总结一下

json.MarshalerRawMessageRawMessagejson.Marshalervar TheInterface = *CustomStruct{}_var _ TheInterface = *CustomStruct{}