I agree with Robert, this is not re usable. I much prefer this
func Sum(some []K, add func(l,r K) K) (ret K) { for _, v := range some { ret = add(ret, v) } return ret } func main(){ total := Sum([]int{1,2,3,4}, func(l,r int) int {return l+r}) } On Tuesday, September 18, 2018 at 5:59:18 PM UTC+2, ohir wrote: > > On Tue, 18 Sep 2018 08:26:29 -0700 (PDT) > alan...@gmail.com <javascript:> wrote: > > > There's no way that your generic Sum function can deal with big.Int > > Oh, with CGG (https://github.com/ohir/gonerics) of course there is: > > func (x type []K) Sum() (r type K) { > for type switch { > case K range int64(), uint64(), float64(), complex128(): > for _, v := range x { > r += v > } > case K big.Int: > for _, v := range x { > r.Add(r,v) > } > break // or return in every case instead > } > return > } > > :) Your welcome. > > > Alan > > > > -- > Wojciech S. Czarnecki > << ^oo^ >> OHIR-RIPE > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.