So in 2012 Cyril Oblikov wrote

Why isn't this code correct?

var N int = ...
for i := range N {
    doSmth(i)
}

In my opinion it looks much simpler than:

var N int = ...
for i := 0; i < N; i++ {
    doSmth(i)
}

So we should say to Cyril (whether he is today), that it is now correct.
You just needed to wait around for a 12 years.
Because you were just ahead of your time....

And if you are reading these lines, please post again to give us a glimpse 
of what new features we should expect to be added to Go in 2036...

- amnon

On Saturday 24 February 2024 at 01:20:04 UTC Duncan Harris wrote:

> I made some changes to use range over int in our code base and was 
> pleasantly surprised with the improvement in readability.
>
> We had quite a few instances where the number of iterations involved a 
> function call which we don't want to repeat:
>
> - for i, n := 0, f(); i < n; i++ {
> + for i := range f() {
>
> A surprising amount where we no longer need the range variable including 
> some nested loops:
>
> - for repeat := 0; repeat < col.Repeat; repeat++ {
> -  for j := 0; j < len(cats); j++ {
> -  for k := 0; k < numStats; k++ {
> + for range col.Repeat {
> +  for range len(cats) {
> +  for k := range numStats {
>
> There were also all the benchmarks:
>
> - for n := 0; n < b.N; n++ {
> + for range b.N {
>
> We could update the docs in https://pkg.go.dev/testing accordingly :-)
>
>
> On Friday 23 February 2024 at 06:18:33 UTC Henry wrote:
>
> This is one feature that provides little value beyond saving a few 
> keystrokes and looking slightly nice, but with potential of increased 
> complexity when we need to implement more important features to the 
> language down the road. This is my opinion. It shouldn't have been added, 
> but what was done is done. There is nothing we can do about it now. 
>
> On Sunday, February 18, 2024 at 5:38:25 AM UTC+7 poweredb...@gmail.com 
> wrote:
>
> I agree with you.
>
>
> Powered By Citizen 
>
> On Saturday, February 17, 2024, 1:19 AM, Kurtis Rader <
> kra...@skepticism.us> wrote:
>
> It's not just changing `k` inside the loop body that makes the 
> transformation invalid -- your observation also applies to modifying `i` 
> inside the loop body. Modifying either variable inside the loop body is 
> extremely rare in my experience and doing so warrants a "dragons be here" 
> comment. Still, your point is valid and anyone applying such a 
> transformation should carefully evaluate the body of each loop to see if 
> either variable might be modified by the loop body. Obviously it would be 
> nice if a tool automated that analysis and transformation but I'm not going 
> to hold my breath waiting for someone to implement that tool.
>
> On Fri, Feb 16, 2024 at 11:06 PM Patrick Smith <pat42...@gmail.com> wrote:
>
> On Fri, Feb 16, 2024 at 10:27 PM Amnon <amn...@gmail.com> wrote:
>
> But now it is out, I think it is great, and have run 
>     perl -pi -e 's/for (\w+) := 0; \1 < ([\w()]+); \1\+\+/for \1 := range 
> \2/' $(git grep -l for) over my entire codebase to use it everywhere.
>
>
> You know your own codebase, and maybe this was safe for you to do. But in 
> general, blindly applying such a blanket change has the potential to cause 
> bugs, as these two are not always equivalent:
>
> for i := range k
> for i := 0; i < k; i++ 
>
> In particular, if k is changed inside the loop they may be very different. 
> https://go.dev/play/p/kAHcmu7377I
>
> (Yes, many people, myself included, would consider changing k inside such 
> a loop to be bad coding style. But "bad style" doesn't mean it's not going 
> to be done.)
>
> -- 
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAADvV_trGNxeLUma722wk-JOGnz42fJqM8%3DVZ36TKpr5s-%3DmOQ%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CAADvV_trGNxeLUma722wk-JOGnz42fJqM8%3DVZ36TKpr5s-%3DmOQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
>
>
> -- 
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>
> -- 
> 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.
>
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD9Hyjj_N9KjY6XfFH0ZUt55f27VCcZQSj_DyX36gzrX1g%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD9Hyjj_N9KjY6XfFH0ZUt55f27VCcZQSj_DyX36gzrX1g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ee355172-59a4-423f-8287-56010883c97cn%40googlegroups.com.

Reply via email to