gotemplate icon indicating copy to clipboard operation
gotemplate copied to clipboard

generate single file for multiple data type

Open oliveagle opened this issue 7 years ago • 1 comments

package max

// template type Max(A)

type A int

func Max(x, y A) A {
	if x < y {
		return y
	}
	return x
}

for above template, is ok to generate multiple files.

//go:generate gotemplate "xxx/max" "MaxInt(int)"
//go:generate gotemplate "xxx/max" "MaxInt8(int8)"
//go:generate gotemplate "xxx/max" "MaxInt16(int16)"
//go:generate gotemplate "xxx/max" "MaxInt32(int32)"
//go:generate gotemplate "xxx/max" "MaxInt64(int64)"

since the template is too simple, it's better to organize these functions into a single file.

is it possible to accept multiple arguments something like this?

// go:generate gotemplate -out "MaxInts" "xxx/max" "MaxInt(int)" "MaxInt8(int8)" "MaxInt16(int16)" "MaxInt32(int32)" "MaxInt64(int64)"

oliveagle avatar Jul 25 '18 05:07 oliveagle

This isn't possible at the moment, but it doesn't sound too difficult... Fancy sending a PR?

ncw avatar Jul 30 '18 22:07 ncw