从锁定的sync.Mutex中恢复

I am attempting to unlock a mutex if it is locked. However, that produced a runtime error so I thought that I would use the recover method:

package main

import "sync"

func main() {
    var l sync.Mutex
    l.Lock()
    l.Unlock()

    defer func() {
        if recover() != nil {
            // the return result can be altered 
            // in a defer function call

        }
    }()

    l.Unlock()  

}

However, even with recover, I still get:

fatal error: sync: unlock of unlocked mutex

You can defer Unlock the Mutex immediately after locking it so you don't have to remember to unlock it for every return path exiting the function.

Delete l.Unlock under l.Lock() or change it for

defer l.Unlock()

and delete 2nd l.Unlock