Jsmith
Jsmith

Reputation: 615

Golang: Values containing the types defined in this package should not be copied

The link https://golang.org/pkg/sync/ states "Values containing the types defined in this package should not be copied."

Why is it so? What would happen if I ignore the advice ?

Upvotes: 3

Views: 212

Answers (1)

Franck Jeannin
Franck Jeannin

Reputation: 6844

It would simply not work as a synchronization primitive anymore. At best you would get unpredictable behavior. Think of a lock (for instance) as a flag. If it is locked, the flag is set. If you copy that lock (you copy the flag's state), the copy would behave as if it was locked while it’s not. If you unlock the copy, the original won’t change so it will behave as if it was locked while it’s not supposed to be locked anymore.

Upvotes: 4

Related Questions