Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/go: improve generating files #35195

Closed
bkielbasa opened this issue Oct 27, 2019 · 3 comments
Closed

proposal: cmd/go: improve generating files #35195

bkielbasa opened this issue Oct 27, 2019 · 3 comments
Labels
Milestone

Comments

@bkielbasa
Copy link

@bkielbasa bkielbasa commented Oct 27, 2019

I don't have an answer on how exactly it the generating should look like but I'm thinking about situations when generics are useful to me.

Interfaces are some kind of generics but they don't cover all the cases. Let's consider the function below

func sortInt(in []int)[]int {
	//...
}

To implement sorting for float32, float64 etc I have to basically copy&paste the code and change types. To make it easier I can use go generate to automate it.

Writing another sub-program in my project which generates these functions shoulds a bit hacky to me and a bit too complicated compared to what I want to achieve.

What I want to suggest to create a simpler way to do so. What? I don't have a good answer to this question yet. One of my ideas is providing a new package that accepts a template and path to a file and does the generation for me.

go generate -from-file file.tmpl -target somefile.go

The file.tmpl file would be the Go template. Parameters would be bounded from the command line arguments.

go generate -from-file file.tmpl -target somefile.go -arg argName=value1,value2,value3
@bkielbasa bkielbasa changed the title (Proposal) Improve generating files proposal: Improve generating files Oct 27, 2019
@gopherbot gopherbot added this to the Proposal milestone Oct 27, 2019
@gopherbot gopherbot added the Proposal label Oct 27, 2019
@gertcuykens

This comment has been minimized.

Copy link
Contributor

@gertcuykens gertcuykens commented Oct 28, 2019

I understand what you are trying to achieve but once we have https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md we dont need it anymore right? So I wouldn't make go generate itself more complex than it has to be and instead create a custom cmd tool.

@ianlancetaylor ianlancetaylor changed the title proposal: Improve generating files proposal: cmd/go: improve generating files Oct 28, 2019
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 28, 2019

There are existing programs that do what you want, such as https://github.com/cheekybits/genny.

I don't understand what you are proposing. A separate program that can be invoked by go:generate? That doesn't have to be a proposal. Just write the program you want.

@bkielbasa

This comment has been minimized.

Copy link
Author

@bkielbasa bkielbasa commented Oct 31, 2019

I was not thinking about any external tool but about improving the generate command. But I see your point. Closing :)

@bkielbasa bkielbasa closed this Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.