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

text/template: add a slice function to the predefined global functions #30153

Closed
a8m opened this issue Feb 10, 2019 · 10 comments
Closed

text/template: add a slice function to the predefined global functions #30153

a8m opened this issue Feb 10, 2019 · 10 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Proposal Proposal-Accepted
Milestone

Comments

@a8m
Copy link
Contributor

a8m commented Feb 10, 2019

I'm suggesting to add a function named slice to the predefined global functions under the text/template package that will mimic the slice expression in Go.

Calling slice returns the result of slicing its first argument by the following arguments.
Thus {{ slice x 1 3 }} is, in Go syntax, x[1:3]. Each sliced item must be a string, slice, or array.

@mvdan
Copy link
Member

mvdan commented Feb 10, 2019

Would three-index slice expressions be supported, such as {{ slice x 1 2 3 }} for x[1:2:3]?

Do you have any data or research to back the need/popularity of such a builtin function? We do have index, but I presume slice wouldn't be nearly as widely used in templates. For example, I think this is the first time anyone has asked about a slice function.

@mvdan mvdan added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 10, 2019
@mvdan mvdan changed the title template/text: add a slice function to the predefined global functions text/template: add a slice function to the predefined global functions Feb 10, 2019
@gopherbot
Copy link

Change https://golang.org/cl/161762 mentions this issue: text/template: add a slice function to the predefined global functions

@a8m
Copy link
Contributor Author

a8m commented Feb 10, 2019

Would three-index slice expressions be supported, such as {{ slice x 1 2 3 }} for x[1:2:3]?

I don't think the three-index form is a real use case for templates.

Do you have any data or research to back the need/popularity of such a builtin function?

I didn't do any research before, just came up with my real use case. I use templates extensively, and many times find it missing for slicing/substring strings and slices.

Little searching in the web, led to find these few links:

Anyway, I don't mind leave it open and see what others think about this.

@robpike robpike added Proposal and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Feb 10, 2019
@robpike
Copy link
Contributor

robpike commented Feb 10, 2019

This is so easy to do with a user-defined function that I don't see the need to build it in.
Making this a proposal so it is properly reviewed.

@mvdan mvdan changed the title text/template: add a slice function to the predefined global functions Proposal: text/template: add a slice function to the predefined global functions Feb 10, 2019
@gopherbot gopherbot added this to the Proposal milestone Feb 10, 2019
@ianlancetaylor ianlancetaylor changed the title Proposal: text/template: add a slice function to the predefined global functions proposal: text/template: add a slice function to the predefined global functions Feb 11, 2019
@rsc
Copy link
Contributor

rsc commented Feb 13, 2019

It does seem hard to justify having index and len (and range) but not slice.
(I am prepared to justify not having cap.)

@bcmills bcmills added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Mar 1, 2019
@andybons
Copy link
Member

Per discussions with @golang/proposal-review, it seems like we should do this. @robpike any final thoughts?

@robpike
Copy link
Contributor

robpike commented Mar 27, 2019

The justification is that the template language is not Go. I still think it's a rare usage in a template and easily done by a user-defined function, but if the sentiment is against me, so be it.

But the template language really isn't Go.

@andybons
Copy link
Member

Accepting per further discussion with @robpike and @golang/proposal-review.

@a8m
Copy link
Contributor Author

a8m commented Apr 22, 2019

These are great news.
I've already implemented it that time, so I'd love to get a review, and fix it if needed.

Just for my curiosity, how does the "proposal-review" work? is it public?

@andybons
Copy link
Member

@a8m more info can be found in https://github.com/golang/proposal/blob/master/README.md

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label May 1, 2019
@rsc rsc modified the milestones: Go1.13, Go1.14 May 1, 2019
@rsc rsc changed the title proposal: text/template: add a slice function to the predefined global functions text/template: add a slice function to the predefined global functions May 1, 2019
@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label May 1, 2019
meowsbits added a commit to meowsbits/go-openrpc that referenced this issue Oct 3, 2019
This has been moved to utils/.

Just for fun, not sure where this went:
golang/go#30153
@golang golang locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Proposal Proposal-Accepted
Projects
None yet
Development

No branches or pull requests

7 participants