Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree that the number of possible solutions is often smaller than in other languages. But there are still often multiple ways which are considered idiomatic Go (in the sense that they are all widely used). E.g. if you build some iterable type, you could provide iteration via:

1. Go channels: send every value over a channel.

2. Callbacks: provide an iteration function which takes a callback function.

3. Closures: let a closure return the next value when called.

4. Iterators: create a separate iterator type with e.g. HasNext()/Next() methods.

In this respect C was much simpler, because only (2) and (4) were really possible (and sometimes indexing).



1 is a misconception. Go channels are fundamentally useless for iteration, because you leak goroutines with break or return inside the loop. Same goes for 3 probably. 2 and 4 are the idiomatic ways (and afaict the only ones used in the stdlib).

edit: Okay, 3 should work too, sorry.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: