title: Assign empty slice without referring to its type?
body: <p>My code calls a library function which looks roughly like this:</p>
  <pre><code>func Search() ([]myLibrary.SomeObject, error) {
      var results []apiv17.SomeObject
      // ...
      if (resultsFound) {
        results = append(results, someResult)
      }
      return results
  }
  </code></pre>
  <p>...and my code calls it and then marshals it to JSON.</p>
  <pre><code>results, err := myLibrary.Search()
  bytes, err := json.Marshal(results)
  </code></pre>
  <p>Now the problem is that because of the way the <code>Search</code> function is written (and let's assume we can't change it), it'll return an uninitialized <code>nil</code> slice if there are no results. And unfortunately, there is no way to configure <code>encoding/json</code> to encode nil slices as <code>[]</code> (see e.g. this <a href="https://github.com/golang/go/issues/37711" rel="nofollow noreferrer">proposal</a> with ongoing discussion).</p>
  <p>Explicitly checking for <code>nil</code> solves the problem:</p>
  <pre><code>results, err := myLibrary.Search()
  if results == nil {
    results = []apiv17.SomeObject{}
  }
  bytes, err := json.Marshal(results)
  </code></pre>
  <p>...but it also adds an explicit dependency on the return type, <code>apiv17.SomeObject</code>. That's inconvenient because that type frequently changes in the library. E.g. in the next library version it might be <code>apiv18.SomeObject</code>.</p>
  <p>With the <code>nil</code> check above, I'll have to update my code every time that happens.</p>
  <p>Is there any way to avoid this and assign an empty, non-nil slice to the variable without explicitly referring to its type? Something like this:</p>
  <pre><code>results = [](type of results){}
  </code></pre>