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

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

Comments

Projects
None yet
7 participants
@a8m
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Mar 27, 2019

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

@robpike

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.