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

Comments

@a8m
Copy link
Contributor

@a8m 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 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 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

@gopherbot gopherbot commented Feb 10, 2019

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 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 labels Feb 10, 2019
@robpike
Copy link
Contributor

@robpike 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 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.)

@andybons
Copy link
Member

@andybons andybons commented Mar 27, 2019

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

@robpike
Copy link
Contributor

@robpike 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

@andybons andybons commented Apr 17, 2019

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

@andybons andybons modified the milestones: Proposal, Go1.13 Apr 17, 2019
@a8m
Copy link
Contributor Author

@a8m 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

@andybons andybons commented Apr 24, 2019

@rsc rsc added the NeedsFix 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 label May 1, 2019
@gopherbot gopherbot closed this in e2970a4 May 23, 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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.