Am Donnerstag, 12. Januar 2017 15:57:40 UTC+1 schrieb Jens Hausherr: > > Hi, > > here is some code that puzzles me. I recently had defined an interface in > a project and had a struct implementing the interface an including > "sync.RWMutex" at the same time. > > Sample Code (running): https://play.golang.org/p/DExfr3Izao > > If you execute this code it just runs fine. If I "go vet" it I get the > following warning: > > test.go:16: Foo passes lock by value: main.Bar > > If I implement the Foo function with a pointer receiver the compile fails > (if I try to assign a Baz instance to 'var x Foo') with the message the Baz > does not implement Foo. > > The only way that works without complaints from compiler or vet is this: > > type Baz struct { > m *sync.Mutex > } > > I can then implement the Interface with a value receiver without > complaints by go vet - but at the price of having an extra initialization > step for my struct. > > Anybody can provide more insight on what/which tool is right? >
As stated in the documentation a sync.Mutex must not be copied, so you cannot it make part of a value you intend to be copied, so vet is "right" here. Just use a pointer to the Mutex. V. > > Thanks for any information, > Jens > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.