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

Comments

@bkielbasa
Copy link

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
@gertcuykens
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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

@golang golang locked and limited conversation to collaborators Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants