判断某个类型是否实现了Lock接口

判断某个类型是否实现了Lock接口

Content #

下面的代码就是确定什么类型会被分析,其实就是实现了 Lock/Unlock 两个方法的 Locker 接口:

var lockerType *types.Interface
// Construct a sync.Locker interface type.
func init() {
    nullary := types.NewSignature(nil, nil, nil, false) // func()
    methods := []*types.Func{
        types.NewFunc(token.NoPos, nil, "Lock", nullary),
        types.NewFunc(token.NoPos, nil, "Unlock", nullary),
    }
    lockerType = types.NewInterface(methods, nil).Complete()
}

Viewpoints #

From #

03|Mutex:4种易错场景大盘点