FAQ
Hi,

I'd like to print the length of an array + 1 in a template, but it seems
that's not possible, or I'm missing some line in the specs?

What I'm trying to do, and which is obviously wrong:
http://play.golang.org/p/1HrXbu9ujj

Anyone has an idea on how to do this if possible?

--
Francesc

--

Search Discussions

  • Joubin Houshyar at Oct 19, 2012 at 7:58 pm

    On Friday, October 19, 2012 2:39:18 PM UTC-4, Francesc Campoy Flores wrote:
    Hi,

    I'd like to print the length of an array + 1 in a template, but it seems
    that's not possible, or I'm missing some line in the specs?
    You can bind funcs to the template. See spec below and (a bit lower) an
    example.
    http://golang.org/pkg/text/template/#FuncMap


    What I'm trying to do, and which is obviously wrong:
    http://play.golang.org/p/1HrXbu9ujj

    Anyone has an idea on how to do this if possible?

    --
    Francesc
    --
  • Francesc Campoy Flores at Oct 19, 2012 at 8:11 pm
    Thanks, I saw that, but I feel a bit weird adding so much boilerplate for
    adding one to a number :-/
    On Fri, Oct 19, 2012 at 11:58 AM, Joubin Houshyar wrote:


    On Friday, October 19, 2012 2:39:18 PM UTC-4, Francesc Campoy Flores wrote:

    Hi,

    I'd like to print the length of an array + 1 in a template, but it seems
    that's not possible, or I'm missing some line in the specs?
    You can bind funcs to the template. See spec below and (a bit lower) an
    example.
    http://golang.org/pkg/text/template/#FuncMap


    What I'm trying to do, and which is obviously wrong:
    http://play.golang.org/**p/1HrXbu9ujj<http://play.golang.org/p/1HrXbu9ujj>

    Anyone has an idea on how to do this if possible?

    --
    Francesc

    --


    --
    --
    Francesc

    --
  • Joubin Houshyar at Oct 19, 2012 at 9:19 pm

    On Friday, October 19, 2012 3:48:34 PM UTC-4, Francesc Campoy Flores wrote:
    Thanks, I saw that, but I feel a bit weird adding so much boilerplate for
    adding one to a number :-/
    It's not that bad.

    http://play.golang.org/p/4zgALysuoE

    On Fri, Oct 19, 2012 at 11:58 AM, Joubin Houshyar <[email protected]<javascript:>
    wrote:

    On Friday, October 19, 2012 2:39:18 PM UTC-4, Francesc Campoy Flores
    wrote:
    Hi,

    I'd like to print the length of an array + 1 in a template, but it seems
    that's not possible, or I'm missing some line in the specs?
    You can bind funcs to the template. See spec below and (a bit lower) an
    example.
    http://golang.org/pkg/text/template/#FuncMap


    What I'm trying to do, and which is obviously wrong:
    http://play.golang.org/**p/1HrXbu9ujj<http://play.golang.org/p/1HrXbu9ujj>

    Anyone has an idea on how to do this if possible?

    --
    Francesc

    --


    --
    --
    Francesc
    --
  • Rob Pike at Oct 19, 2012 at 9:35 pm
    It's a slippery slope thing. Sure, +1 is easy but then you want *2 and
    then <<3 and then switch and then closures and so on. Unlike with
    templates in other languages, I deliberately chose not to provide a
    full programming language, and ask the programmer to use the one
    that's already there.

    Functions are underappreciated. In general, not just in templates.

    -rob

    --
  • Joubin Houshyar at Oct 19, 2012 at 10:13 pm

    On Fri, Oct 19, 2012 at 5:27 PM, Rob Pike wrote:

    It's a slippery slope thing. Sure, +1 is easy but then you want *2 and
    then <<3 and then switch and then closures and so on. Unlike with
    templates in other languages, I deliberately chose not to provide a
    full programming language, and ask the programmer to use the one
    that's already there.

    Functions are underappreciated. In general, not just in templates.
    Possibility remains to beef up the builtins, and/or export a select subset
    (e.g. truth -> Truth) as helpers at the package level. The example just
    posted is not doing any type checking, etc. as it
    should<http://golang.org/src/pkg/text/template/funcs.go#L143>.
    I think end users would benefit from a refactored length to call a
    Container (akin to truth) that can be used to build up their specific
    required funcs. "Batteries included".

    -rob
    --
  • Steven Blenkinsop at Oct 19, 2012 at 11:38 pm
    On Friday, October 19, 2012, Joubin Houshyar wrote:
    On Friday, October 19, 2012 3:48:34 PM UTC-4, Francesc Campoy Flores wrote:

    Thanks, I saw that, but I feel a bit weird adding so much boilerplate for
    adding one to a number :-/
    It's not that bad.

    http://play.golang.org/p/4zgALysuoE
    It's not even that bad. You can just return the number:

    http://play.golang.org/p/X7iTnJ5Nrm

    You can even use a simpler, more general function which you can then
    compose with other functions to get the same result:

    http://play.golang.org/p/jEA_red64E

    --
  • Francesc Campoy Flores at Oct 19, 2012 at 11:42 pm
    I actually agree with minimizing the power of the expressions you could use
    on templates. I don't want to end up writing php-like templates.

    Thanks for your solutions, I actually ended up creating a new method on the
    object I was passing generating the value I wanted, which is pretty similar
    to what you proposed, but I avoid adding functions to the map.

    Thanks!
    On Fri, Oct 19, 2012 at 4:38 PM, Steven Blenkinsop wrote:
    On Friday, October 19, 2012, Joubin Houshyar wrote:



    On Friday, October 19, 2012 3:48:34 PM UTC-4, Francesc Campoy Flores
    wrote:
    Thanks, I saw that, but I feel a bit weird adding so much boilerplate
    for adding one to a number :-/
    It's not that bad.

    http://play.golang.org/p/4zgALysuoE
    It's not even that bad. You can just return the number:

    http://play.golang.org/p/X7iTnJ5Nrm

    You can even use a simpler, more general function which you can then
    compose with other functions to get the same result:

    http://play.golang.org/p/jEA_red64E

    --



    --
    --
    Francesc

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 19, '12 at 7:37p
activeOct 19, '12 at 11:42p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase