deckarep / golang-set

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.
Other
4k stars 272 forks source link

refactored Pop() function on threadUnsafeSet #108

Closed notEpsilon closed 1 year ago

notEpsilon commented 1 year ago

old

func (s *threadUnsafeSet[T]) Pop() (v T, ok bool) {
    for item := range *s {
        delete(*s, item)
        return item, true
    }
    return
}

in my opinion, the last return badly impacts readability.

refactored

func (s *threadUnsafeSet[T]) Pop() (T, bool) {
    for item := range *s {
        delete(*s, item)
        return item, true
    }
    return *new(T), false
}

test results

$ go test ./... -cover -race
ok      github.com/deckarep/golang-set/v2       79.012s coverage: 94.8% of statements