how to use go generate to generate generic code .

881 views
Skip to first unread message

hui zhang

unread,
Mar 10, 2017, 3:29:20 AM3/10/17
to golang-nuts


I want to generate a min func 

func Min(a,b T) T {
if a < b {
return a
} else {
return b
}
}

pls generate below, pls use go generate to generate  int int64 int32 int16.... uint64 uint32 ....  float32 float64 
Pls do not just paste documents.   There is a lot on google. but none of them explain it clearly
Just tell me how to do the below things generate by demo.


func MinInt32(a,b int32) int32 {
if a < b {
return a
} else {
return b
}
}
func Minint(a,b int) int {
if a < b {
return a
} else {
return b
}
}
func Minfloat32(a,b float32) float32 {
if a < b {
return a
} else {
return b
}
}

............






hui zhang

unread,
Mar 10, 2017, 3:35:45 AM3/10/17
to golang-nuts
And pls don't tell me interface{} is good , can solve your problem bla bla bla
when interface{} meet slice 
[]int -->  []interface{} , 
all generic can not be done

在 2017年3月10日星期五 UTC+8下午4:29:20,hui zhang写道:

Egon

unread,
Mar 10, 2017, 4:24:55 AM3/10/17
to golang-nuts
On Friday, 10 March 2017 10:29:20 UTC+2, hui zhang wrote:
I want to generate a min func 

Why? That doesn't sound like a good use of time. :P
You would probably much faster using keyboard macros or multiple cursors e.g. (real-time)


I'm sure you were solving some other problem than just implementing Min*?

Having multiple such func-s is usually a design smell, use one type: int, int32 or int64 and be done with it, rather than trying to implement everything in the universe.

If you do decide to go the generation route, which I wouldn't suggest, then see https://blog.golang.org/generate and use text/template https://play.golang.org/p/LCE806P-UJ.

There are of course plenty of tools to help you with generation:

+ Egon

Ignazio Di Napoli

unread,
Mar 10, 2017, 6:54:04 AM3/10/17
to golang-nuts
Wow. :)

Henry

unread,
Mar 10, 2017, 8:14:10 AM3/10/17
to golang-nuts
If you insist on generics-like code generation:
https://github.com/cheekybits/genny/blob/master/README.md

Tyler Compton

unread,
Mar 10, 2017, 7:27:49 PM3/10/17
to Henry, golang-nuts
If we assume a more useful generic task than writing a min function, what's wrong with using code generation?

On Fri, Mar 10, 2017 at 6:14 AM Henry <henry.ad...@gmail.com> wrote:
If you insist on generics-like code generation:
https://github.com/cheekybits/genny/blob/master/README.md

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Jones

unread,
Mar 10, 2017, 7:37:04 PM3/10/17
to Tyler Compton, Henry, golang-nuts
Nothing wrong with it, and nothing wrong with doing it for min and max either. (Though it was a delight to see that animated editing sequence, so for entertainment value that's the way!)

I did code generation for my version of Sort and it is 3x-5x faster. It takes intel more than 10 years to make a CPU that is 3-5x faster. Takes just one programmer hour to perfect it with code generation. Nothing wrong with that equation. 

Let them laugh all they want while you build for speed.

Michael

On Fri, Mar 10, 2017 at 4:27 PM, Tyler Compton <xav...@gmail.com> wrote:
If we assume a more useful generic task than writing a min function, what's wrong with using code generation?
On Fri, Mar 10, 2017 at 6:14 AM Henry <henry.ad...@gmail.com> wrote:
If you insist on generics-like code generation:
https://github.com/cheekybits/genny/blob/master/README.md

--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones
michae...@gmail.com

prade...@gmail.com

unread,
Mar 10, 2017, 10:13:26 PM3/10/17
to golang-nuts
You might want to reconsider your use of Go if the lack of generics hinders your productivity. Code generation, IDE macros and other tricks do not solve this problem. 
Sometimes go is just not the right tool for the job. D has generics and solid concurrency primitives. 

Egon

unread,
Mar 11, 2017, 9:32:41 AM3/11/17
to golang-nuts, henry.ad...@gmail.com
On Saturday, 11 March 2017 02:27:49 UTC+2, Tyler Compton wrote:
If we assume a more useful generic task than writing a min function, what's wrong with using code generation?

There's nothing wrong with in it of itself. It's a tool like any other.

Based on the limited information presented about the problem, it seems using code generation is overkill. And also needs more work.

1. Using a single type (e.g. float32) throughout would mean much less code, overall. Whether this approach is viable depends on the context, but I've often seen "generic" solution to a problem that doesn't have to be generic in the first place. Generic code can often end up very vague and with a poor API, due to the nature of trying to do multiple things at once.

2. If the intent was just to implement Min/Max, there are easier alternatives, as demonstrated by the gif. Quite often I end up using that approach instead of code-generation or generics for that matter, because it is much easier to reason about and gets the job done quicker. (of course, it should go without saying, it has its downsides)

3. Depending on the context, there are existing solutions, which would mean you don't have to write the code in the first place.

My default position is -- you don't need generics, code generation... Of course, there are places where you need them.

+ Egon

Сергей Камардин

unread,
Mar 11, 2017, 9:37:24 AM3/11/17
to golang-nuts
Hi! You also could use C preprocessor to make such things. I have used it to generate some generics algorithms to increase performance and reduce interfaces overhead. It is not the best approach, but it could be useful and powerful.

FYI: https://github.com/gobwas/ppgo

Michael Jones

unread,
Mar 11, 2017, 9:51:16 AM3/11/17
to Сергей Камардин, golang-nuts
Very nice! Just the kind of use that comes up for me.

The one complexity I had when I did this for the standard sort was dealing with the float32 and float64 types, where comparison was more complicated and elaborate in the presence of NaN values. Still, this kind of compile-time type templating is the only kind of generic that i ever need.

On Sat, Mar 11, 2017 at 6:37 AM, Сергей Камардин <gob...@gmail.com> wrote:
Hi! You also could use C preprocessor to make such things. I have used it to generate some generics algorithms to increase performance and reduce interfaces overhead. It is not the best approach, but it could be useful and powerful.

FYI: https://github.com/gobwas/ppgo
--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tong Sun

unread,
Mar 23, 2017, 10:28:50 AM3/23/17
to golang-nuts, xav...@gmail.com, henry.ad...@gmail.com
Quite agree. 

code generation will be especially helpful if it is not something trivial, and you need several rounds to make it perfect. 

@hui zhang

If you need something generic, take a look at https://github.com/go-easygen/easygen

For e.g., here is how I generate the command line CLI handling:



On Friday, March 10, 2017 at 7:37:04 PM UTC-5, Michael Jones wrote:
Nothing wrong with it, and nothing wrong with doing it for min and max either. (Though it was a delight to see that animated editing sequence, so for entertainment value that's the way!)

I did code generation for my version of Sort and it is 3x-5x faster. It takes intel more than 10 years to make a CPU that is 3-5x faster. Takes just one programmer hour to perfect it with code generation. Nothing wrong with that equation. 

Let them laugh all they want while you build for speed.

Michael
On Fri, Mar 10, 2017 at 4:27 PM, Tyler Compton <xav...@gmail.com> wrote:
If we assume a more useful generic task than writing a min function, what's wrong with using code generation?
On Fri, Mar 10, 2017 at 6:14 AM Henry <henry.ad...@gmail.com> wrote:
If you insist on generics-like code generation:
https://github.com/cheekybits/genny/blob/master/README.md

--
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...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Henry

unread,
Mar 27, 2017, 3:33:38 AM3/27/17
to golang-nuts
Actually, it would be quite nice to simplify the Go's AST package and makes it simpler for people to develop custom code generation and tools. I use code generation quite a lot, eg. to generate data-access code, etc., but the process of creating one requires some effort to get right. I think go generate shows a lot of promise and it is currently underused. It is probably one area where Go can potentially outshine other programming languages.

Paul Jolly

unread,
Mar 27, 2017, 5:19:07 AM3/27/17
to Henry, golang-nuts
Of course you don't need to us go/ast for output (it's very likely used heavily when working out what to generate based on Go source input) - I generally create some simple helper types that wrap a bytes.Buffer, generate most code using text/template into said buffer, format using golang.org/x/tools/imports, then write only if the file on disk is different from my buffer contents.

--
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+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages