News for hackers. Stuff that matters.
Re:Experiences Using Go as a Teaching Language with Young Pr... by laumars on Thursday August 13, 2015 @10:04PM (#10058271)
> Well, yeah, but if you want to iterate over integer ranges, don't you use the for ;; {} form? (If not why is that form even there?)

for range does iterate over integer arrays / slices. The reason the C-style for loop exists is because sometimes you don't want to iterate the entire stack. eg

    // iterate half the stack:
    for i := 0; i < len(slice) / 2; i++ {}

    // iterate with a stepping:
    for i := 0; i < len(slice); i+=2 {}

    // iterate from a different starting position:
    for i := 5; i < len(slice); i++ {}

    // reverse iteration:
    for i := len(slice)-1; i>=0; i-- {}
Re:Experiences Using Go as a Teaching Language with Young Pr... by infogulch on Thursday August 13, 2015 @11:36PM (#10058507)
Slicing is free and can be done inline, so your first and third examples can be changed to the range syntax:

    // iterate half the stack:
    for i := range slice[:len(slice)/2] {}
    
    // iterate from a different starting position:
    for i := range slice[5:] {}
That still leaves stepping and reverse, but I've found those are relatively rare, especially compared to the other two.
Re:Experiences Using Go as a Teaching Language with Young Pr... by laumars on Tuesday August 18, 2015 @04:08AM (#10078037)
Ooops, just seen this comment. Yes, you're right, your slice techniques do make a lot more sense than my examples.