Search code examples
gostatic-analysis

Function returns lock by value


I have the following structure

    type Groups struct {
        sync.Mutex
        Names []string
    }

and the following function

    func NewGroups(names ...string) (Groups, error) {
        // ...
        return groups, nil
    }

When I check for semantic errors with go vet, I am getting this warning:

NewGroups returns Lock by value: Groups

As go vet is shouting, it is not good. What problems can this code bring? How can I fix this?


Solution

  • You need to embed the sync.Mutex as a pointer:

    type Groups struct {
        *sync.Mutex
        Names []strng
    }
    

    Addressing your comment on your question: In the article http://blog.golang.org/go-maps-in-action notice Gerrand is not returning the struct from a function but is using it right away, that is why he isn't using a pointer. In your case you are returning it, so you need a pointer so as not to make a copy of the Mutex.

    Update: As @JimB points out, it may not be prudent to embed a pointer to sync.Mutex, it might be better to return a pointer to the outer struct and continue to embed the sync.Mutex as a value. Consider what you are trying to accomplish in your specific case.