title: Should I use panic or return error?
body: <p>Go provides two ways of handling errors, but I'm not sure which one to use.</p>
  
  <p>Assuming I'm implementing a classic <code>ForEach</code> function which accepts a slice or a map as an argument. To check whether an iterable is passed in, I could do:</p>
  
  <pre><code>func ForEach(iterable interface{}, f interface{}) {
      if isNotIterable(iterable) {
          panic("Should pass in a slice or map!")
      }
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>func ForEach(iterable interface{}, f interface{}) error {
      if isNotIterable(iterable) {
          return fmt.Errorf("Should pass in a slice or map!")
      }
  }
  </code></pre>
  
  <p>I saw some discussions saying <code>panic()</code> should be avoided, but people also say that if program cannot recover from error, you should <code>panic()</code>.</p>
  
  <p>Which one should I use? And what's the main principle for picking the right one?</p>