Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents
outlinetrue

...

Slices and maps contain pointers to the underlying data so beĀ conscious of ownership when passing them as parameters or returning them as values.

On the other hand, do not unnecessarily copy slices - heap allocations are expensive and we want to avoid them when possible.

...

Bad

Good

Code Block
languagego
func myFunc() error {
  // do some work...
  p.Lock()
  if err := somethingThatCanPanic(); err != nil {
    p.Unlock()
    return err
  }
  p.Unlock()
  // do some more work...
  return nil
}

// if somethingThatCanPanic() panics, the mutex will be left locked.
Code Block
languagego
func myFunc() error {
  // do some work...
  doWorkdoRiskyWork := func() error {
    p.Lock()
    defer p.Unlock()
    return somethingThatCanPanic()
  }
  if err := doWorkdoRiskyWork(); err != nil {
    return err
  }
  // do some more work...
  return nil
}

// enables us to continue use defer without leaving the mutex locked longer than necessary.

...

The following sub-section are an excerpt from https://cockroachlabs.atlassian.net/wiki/pages/resumedraft.action?draftId=1676804870 . Read the full page for more background and context.

Include Page
Error handling basics
Error handling basicsisMissingRequiredParameterstrue

Performance

Performance-specific guidelines apply only to the hot path.

...