FAQ
I have a loop in my code that just needs to execute a certain number of
times.

I don't make any reference to the loop control variable anywhere in the
body of the loop, but since the for clauses have to test it against the
limit and increment it, I can't use _. So instead, I just used _i, as a
signal that the variable's value was unused. Does that seem reasonable, or
would it be better Go to just leave it as bog-standard i and not worry
about signalling the don't-care condition?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Caleb Spare at Apr 3, 2015 at 1:35 am
    Just use i anyway in those cases. That's what the code in e.g. the standard
    library does. If I saw _i, the nonstandard naming would probably trip me up
    more than a loop variable that's not used within the body of the loop.

    -Caleb
    On Thu, Apr 2, 2015 at 6:29 PM, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Caleb Spare at Apr 3, 2015 at 1:36 am
    Or you can do

    for range [10]struct{}{} {
       // blah
    }

    (Joke; please don't do this.)

    On Thu, Apr 2, 2015 at 6:34 PM, Caleb Spare wrote:

    Just use i anyway in those cases. That's what the code in e.g. the
    standard library does. If I saw _i, the nonstandard naming would probably
    trip me up more than a loop variable that's not used within the body of the
    loop.

    -Caleb
    On Thu, Apr 2, 2015 at 6:29 PM, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mark J. Reed at Apr 3, 2015 at 1:47 am
    Well, obviously if I were going to do that I would use the iter package.
    Much more legible. ;)
    On Thu, Apr 2, 2015 at 9:36 PM, Caleb Spare wrote:

    Or you can do

    for range [10]struct{}{} {
    // blah
    }

    (Joke; please don't do this.)

    On Thu, Apr 2, 2015 at 6:34 PM, Caleb Spare wrote:

    Just use i anyway in those cases. That's what the code in e.g. the
    standard library does. If I saw _i, the nonstandard naming would probably
    trip me up more than a loop variable that's not used within the body of the
    loop.

    -Caleb
    On Thu, Apr 2, 2015 at 6:29 PM, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

    --
    Mark J. Reed <markjreed@gmail.com>

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason Gade at Apr 3, 2015 at 5:37 am
    Holy cow, that works. http://play.golang.org/p/wInRfj9jk_

    Mind. Blown.
    On Thursday, April 2, 2015 at 6:36:55 PM UTC-7, Caleb Spare wrote:

    Or you can do

    for range [10]struct{}{} {
    // blah
    }

    (Joke; please don't do this.)


    On Thu, Apr 2, 2015 at 6:34 PM, Caleb Spare <ces...@gmail.com
    <javascript:>> wrote:
    Just use i anyway in those cases. That's what the code in e.g. the
    standard library does. If I saw _i, the nonstandard naming would probably
    trip me up more than a loop variable that's not used within the body of the
    loop.

    -Caleb

    On Thu, Apr 2, 2015 at 6:29 PM, Mark Reed <mark...@gmail.com
    <javascript:>> wrote:
    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Anlhord at Apr 5, 2015 at 5:38 pm
    http://play.golang.org/p/X6mCEsIFzI
    On Friday, April 3, 2015 at 3:29:15 AM UTC+2, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Apr 5, 2015 at 9:01 pm
    for i := 0; i < 10; i++

    Short, simple, straightforward, clear, obvious, understandable, not a
    puzzle, not cute, not confusing, just right, just do it.

    -rob

    On Sun, Apr 5, 2015 at 10:38 AM, wrote:

    http://play.golang.org/p/X6mCEsIFzI
    On Friday, April 3, 2015 at 3:29:15 AM UTC+2, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Russel Winder at Apr 6, 2015 at 2:44 pm

    On Sun, 2015-04-05 at 14:01 -0700, Rob Pike wrote:
    for i := 0; i < 10; i++

    Short, simple, straightforward, clear, obvious, understandable, not a
    puzzle, not cute, not confusing, just right, just do it.
    Whilst this is the way it has to be done in Go, I think you are wrong
    on all counts. This is why things like:

         10.times

         for _ in 1..10

    and other variants have been chosen in other languages.

    Without experiments from the psychology of programming folk, making
    statements about "understandable" and "not confusing" is simply
    opinion not data.

    --
    Russel.
    =============================================================================
    Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net
    41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk
    London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrey mirtchovski at Apr 6, 2015 at 2:53 pm
    10.times
    10 times what?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Apr 6, 2015 at 2:55 pm

    On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski wrote:

    10.times
    10 times what?

    Please don't feed the trolls.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Russel Winder at Apr 6, 2015 at 3:21 pm

    On Mon, 2015-04-06 at 10:54 -0400, Shawn Milochik wrote:
    On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski <
    mirtchovski@gmail.com>
    wrote:
    10.times
    10 times what?
    Rob's code fragment was:

             for i := 0, i < 10; i++

    without a code body, which was nonetheless implied. I just used the
    same sort of fragment showing the control without the following code
    block.
    Please don't feed the trolls.
    Far from being a troll.

    --
    Russel.
    =============================================================================
    Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net
    41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk
    London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bep at Apr 6, 2015 at 10:38 pm
    Troll is a trademark of Norway. I'm from Norway, I can use it ...

    I like

         for i := 0, i < 10; i++

    It was the first loop construct I learnt. And it still makes sense.

    bep

    mandag 6. april 2015 17.21.50 UTC+2 skrev Russel Winder følgende:
    On Mon, 2015-04-06 at 10:54 -0400, Shawn Milochik wrote:
    On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski <
    mirtc...@gmail.com <javascript:>>
    wrote:
    10.times
    10 times what?
    Rob's code fragment was:

    for i := 0, i < 10; i++

    without a code body, which was nonetheless implied. I just used the
    same sort of fragment showing the control without the following code
    block.
    Please don't feed the trolls.
    Far from being a troll.

    --
    Russel.
    =============================================================================

    Dr Russel Winder t: +44 20 7585 2200 voip: sip:russ...@ekiga.net
    <javascript:>
    41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk
    <javascript:>
    London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andy Balholm at Apr 7, 2015 at 4:08 pm
    My favorite construct for “do this a certain number of times but don’t use the counter for anything else" is REPEAT in Logo:

    REPEAT 4 [ FORWARD 100 RIGHT 90 ]

    But I can’t even remember how to do a loop in Logo where you do use the counter…

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mark J. Reed at Apr 7, 2015 at 4:32 pm
    Logo makes the iteration count available in the loop body as # whether you
    want it or not, whether it's a REPEAT loop or a FOREACH loop (in which the
    current item is also available as ?). If it is an actual REPEAT loop, then
    REPCOUNT also returns the same value as the innermost # (in any other
    context, REPCOUNT returns -1).

    But we're talking about Go without the Lo-. For what it's worth, I had no
    intention of reopening the debate on how to do counting loops; I was just
    asking if s "don't-care" indicator on the lcv was reasonable, and the
    answer seems to be "no", so I'll stick to the basic `for i` form.

    If I were inclined to add anything to the language for more explicit
    support of a pure-counting loop, it would probably be to make "range" work
    on integers, generating a loop from 0 to N-1 for value N. But that would
    only handle one common case and I agree that the ensuing worm-can-opening
    would not be worth it.





    On Tue, Apr 7, 2015 at 12:08 PM, Andy Balholm wrote:

    My favorite construct for “do this a certain number of times but don’t use
    the counter for anything else" is REPEAT in Logo:

    REPEAT 4 [ FORWARD 100 RIGHT 90 ]

    But I can’t even remember how to do a loop in Logo where you do use the
    counter…

    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/G7Cm6wRrVkA/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    Mark J. Reed <markjreed@gmail.com>

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Haddock at Apr 7, 2015 at 7:50 pm

    Am Montag, 6. April 2015 16:44:41 UTC+2 schrieb Russel Winder:
    On Sun, 2015-04-05 at 14:01 -0700, Rob Pike wrote:
    for i := 0; i < 10; i++

    Short, simple, straightforward, clear, obvious, understandable, not a
    puzzle, not cute, not confusing, just right, just do it.
    Whilst this is the way it has to be done in Go, I think you are wrong
    on all counts. This is why things like:

    10.times

    for _ in 1..10

    and other variants have been chosen in other languages.
    +1

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Fatdogbear at Apr 7, 2015 at 6:58 pm
    i thought the same, but then I thought the answer was so simple that I must
    be misunderstanding the question.
    On Sunday, April 5, 2015 at 5:02:22 PM UTC-4, Rob 'Commander' Pike wrote:

    for i := 0; i < 10; i++

    Short, simple, straightforward, clear, obvious, understandable, not a
    puzzle, not cute, not confusing, just right, just do it.

    -rob

    On Sun, Apr 5, 2015 at 10:38 AM, <anl...@gmail.com <javascript:>> wrote:

    http://play.golang.org/p/X6mCEsIFzI
    On Friday, April 3, 2015 at 3:29:15 AM UTC+2, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Piers at Apr 6, 2015 at 7:32 am

    On Sunday, 5 April 2015 18:38:12 UTC+1, anl...@gmail.com wrote:
    http://play.golang.org/p/X6mCEsIFzI
    There's a 'subtle' bug..

    Out of interest I ran it through 'go vet' and golint and they don't report
    any problem.

    Is there a tool or invocation of the existing ones which does report unused
    function parameters?

    I suppose a tool could also report 'suspicious' repeats of the same
    constant, e.g. 10 here, but that might get annoying :)


    On Sunday, 5 April 2015 18:38:12 UTC+1, anl...@gmail.com wrote:

    http://play.golang.org/p/X6mCEsIFzI
    On Friday, April 3, 2015 at 3:29:15 AM UTC+2, Mark Reed wrote:

    I have a loop in my code that just needs to execute a certain number of
    times.

    I don't make any reference to the loop control variable anywhere in the
    body of the loop, but since the for clauses have to test it against the
    limit and increment it, I can't use _. So instead, I just used _i, as a
    signal that the variable's value was unused. Does that seem reasonable, or
    would it be better Go to just leave it as bog-standard i and not worry
    about signalling the don't-care condition?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Apr 6, 2015 at 9:06 am
    It's a feature. "Any caller can have a loop counting any number of iterations that he wants so long as it is 10." – Henry Ford
    On 06/04/2015, at 5:02 PM, "Piers" wrote:

    There's a 'subtle' bug..
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Atdiar at Apr 6, 2015 at 2:28 pm

    On Monday, April 6, 2015 at 11:06:36 AM UTC+2, kortschak wrote:
    It's a feature. "Any caller can have a loop counting any number of
    iterations that he wants so long as it is 10." – Henry Ford
    I could have sworn that quote was from Mahatma Gandhi.

    On 06/04/2015, at 5:02 PM, "Piers" <goo...@o172.net <javascript:>> wrote:

    There's a 'subtle' bug..
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 3, '15 at 1:29a
activeApr 7, '15 at 7:50p
posts19
users15
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase