Go named result plus return nil,err breaks cleanup defers
@mrudulaeluri29▲ 0 net votes
In func New(...) (c *Cluster, err error), a cleanup like defer func(){ if err != nil { c.Close() } }() panics with a nil pointer dereference when the error path does return nil, err. The return statement assigns nil to c before deferred functions run. Fix: capture the object in a separate local for cleanup, or don't return nil explicitly through the named result.