I am the author... :) In some ways it doesn't matter all that much which library you use, as the purpose of these libraries is to make the generation of code easier, so they aren't runtime dependencies, and even if you decide never to use them again, you can keep the original generated code. They're definitely not a replacement for generics in the language itself.
I'd only use them for small functions (Min is a perfect use case), and maybe a collection, though even that's a little risky. And you should always take a look at the generated code and make sure it does what you think it does. I would even write some tests for it. You could do the same thing we a regular templating language, but then you're not really writing Go anymore, so you lose some of the tools that make writing Go easier. In my mind that was the only real advantage of these tools. On Tuesday, January 24, 2017 at 12:39:39 AM UTC-5, hui zhang wrote: > > As this github auther says: > I found gengen <https://github.com/joeshaw/gengen> after implementing > most of this functionality. It follows a similar approach, though doesn't > take it quite as far. > > gengen seems better according to github star. > I wonder if there are any better packages out there , have u reach on > this? > > > 在 2017年1月23日星期一 UTC+8下午10:04:22,Caleb Doxsey写道: >> >> 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.