Hi,

With github.com/badgerodon/goreify you can do this:

//go:generate goreify examples/min.Min numeric

// Min finds the minimum value in a slice
func Min(args ...generics.T1) (r generics.T1) {
if len(args) == 0 {
panic("the minimum of nothing is undefined")
}

r = args[0]
for i := 1; i < len(args); i++ {
if generics.Less(r, args[i]) {
r = args[i]
}
}
return r
}


(replacing examples/min with your package name)

This gives:

// MinInt finds the minimum value in a slice
func MinInt(args ...int) (r int) {
if len(args) == 0 {
panic("the minimum of nothing is undefined")
}

r = args[0]
for i := 1; i < len(args); i++ {
if r < args[i] {
r = args[i]
}
}
return r
}


There are other libraries out there that can do the same thing.

On Monday, January 23, 2017 at 1:19:25 AM UTC-5, hui zhang wrote:
>
> Since go did not support generic yet ,
> Generate give us a alternative choice .
> However,  although I read the official document.  
> I still don't know how to use go generate to generate code.
> Could anyone show us an example ?
>
> Let's take   std::min in c++ us an example.
> min(x,y)       // type could be int int32 int64 int8  uint....   float??
> min(x,y,z,a,b......)  //Variable-length Argument 
>

-- 
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.

Reply via email to