Why do the Go-generated protobuf files contain mutex locks?

The impl.MessageState is embedded in concrete messages only, not in the generated structs that implement a proto message.

It specifically embeds the three pragmas: NoUnkeyedLiterals, DoNotCompare, and DoNotCopy.

The last one, DoNotCopy is a zero-sized array of sync.Mutex. The sole purpose is to have go vet complain loudly about shallow copies, as described in the comment:

DoNotCopy can be embedded in a struct to help prevent shallow copies.
This does not rely on a Go language feature, but rather a special case
within the vet checker.

The summary of it all: impl.MessageState is not supposed to be copied and the mutex is there only to catch copying. If you do so, it is because you are using something the wrong way.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)