title: making generic algorithms in go
body: <p>I can't figure out a clean way to implement an algorithm that will work on any type.</p>
  
  <p>The following code will produce errors trying to convert a string or a typed slice into interfaces, and you can't compare interface{} objects: <code>invalid operation: result[0] &gt; result[n - 1] (operator &gt; not defined on interface)</code></p>
  
  <pre><code>func main() {
      c := Algo("abc")
      //...
      c := Algo([3]int{1,2,3})
      //...
  }
  
  func Algo(list []interface{}) chan []interface{} {
      n := len(list)
      out := make(chan []interface{})
      go func () {
          for i := 0; i &lt; n; i++ {
              result := make([]interface{}, n)
              copy(result, list)
              // an actually useful algorithm goes here:
              if (result[0] &gt; result[n-1]) {
                  result[0], result[n-1] = result[n-1], result[0]
              }
              out &lt;- result
          }
          close(out)
      }()
      return out
  }
  </code></pre>
  
  <p>Although it's a pain (I think it should be automatic), I can manually box and unbox typed slices into interface{}s, the real problem above is the comparison. And it just keeps getting more and more kludgy.</p>
  
  <pre><code>a := [3]int{1,2,3}
  b := make([]interface{}, len(a))
  for i, _ := range a {
      b[i] = a[i]
  }
  </code></pre>
  
  <p>I've even thought of using vector.Vector, but so many people say never to use them.</p>
  
  <p>So should I just implement the same algorithm for int slices and strings?  What about slices of myObject?  I can make an interface with a custom comparison func, but then how do I make it work with standard types?</p>