The question why the spec doesn't allow it is more
complex. It could be allowed I think, but the argument
"looks much simpler" might not convince e.g. a
programmer with a strong C background who thinks
that for i:=0;i<N;i++ looks way more natural and clear.
The two forms are not interchangeable.for i := 0; i < 5; i++ {println(i)i = 5}(prints one line)andfor i := range 5 {println(i)i = 5}would likely do different things. I would assume the latter would work like:for i := range make([]int, 5) {println(i)i = 5}
2012/1/19 Paul Borman <bor...@google.com>The two forms are not interchangeable.for i := 0; i < 5; i++ {println(i)i = 5}(prints one line)andfor i := range 5 {println(i)i = 5}would likely do different things. I would assume the latter would work like:for i := range make([]int, 5) {println(i)i = 5}for i := range [10] struct{}{} {...}:)
well, it is also rather easy to create an 'itertools' package with a Range
function returning a slice with the data, and an IRange function
returning a channel to the data (when the slice begins to be too big)
package itertools
func Range(args ...int) []int {
nargs := len(args)
start, stop, stride := 0, 0, 0
switch nargs {
case 1:
start = 0
stop = args[0]
stride= 1
case 2:
start = args[0]
stop = args[1]
stride= 1
case 3:
start = args[0]
stop = args[1]
stride= args[2]
default:
panic("boo")
}
out := []int{}
for i := start; i < stop; i += stride {
out = append(out, i)
}
return out
}
// EOF //
then:
for i := range itertools.Range(10) {
fmt.Println(i)
}
-s
for i := range [10] struct{}{} {...}:)
well, it is also rather easy to create an 'itertools' package with a RangeOn Thu, 19 Jan 2012 09:05:50 -0800 (PST), Cyril Oblikov <mun...@gmail.com> wrote:
> четверг, 19 января 2012 г. 19:14:46 UTC+3 пользователь Volker Dobler
> написал:
> >
> > The question why the spec doesn't allow it is more
> > complex. It could be allowed I think, but the argument
> > "looks much simpler" might not convince e.g. a
> > programmer with a strong C background who thinks
> > that for i:=0;i<N;i++ looks way more natural and clear.
> >
>
> Maybe, but a programmer with strong Python background probably would prefer
> range :)
> As far as I can see this feature is obvious enough and will not mislead
> anyone.
function returning a slice with the data, and an IRange function
returning a channel to the data (when the slice begins to be too big)
dammit, you got there first!
It's fun, but it's *horrible* coding practice. Kittens will die if you use it.
for i := range [10] struct{}{} {
for i := 0; i < 10; i++ {
Shorter, more flexible, and recognizable since decades ago.
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I'm not absolutely sure of anything.
right, but then the start/stop/stride aren't relevant anymore, are they ?
ie:
for _,v := range itertools.Range(2,10,3) {
fmt.Print("v=",v,"\n")
}
-s
--
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################
> The magical 'struct {}'! This kind of trick might be worth mention inIt's fun, but it's *horrible* coding practice. Kittens will die if you use it.
> effective_go or go-wiki? Or this is considered not that good
> coding practice?
for i := 0; i < 10; i++ {
for i := range [10] struct{}{} {
Shorter, more flexible, and recognizable since decades ago.
It's getting better and better.. :)
for the record, i wasn't proposing that struct{}{} to *be* nil, just
that for it to be compatible with nil (the same as it's compatible
with *Foo interface{} and chan bool, without "being" any of those types).
Hmmm... I am not planning to use this construct, but I am puzzled about
this behaviour of struct{}.
What is the "magical 'struct {}'".
M;
>
> Hmmm... I am not planning to use this construct, but I am puzzled about this
> behaviour of struct{}.
> What is the "magical 'struct {}'".
A struct{} takes up no room.
Chris
--
Chris "allusive" Dollin
func numRange(first int, rest ...int) chan int {out := make(chan int)var start, end intif len(rest) == 0 {start = 1end = first} else {start = firstend = rest[0]}go func() {for i := start; i <= end; i++ {out <- i}close(out)}()return out}
for s := range numRange(5) {
fmt.Println(s)
}for s := range numRange(5, 10) {
fmt.Println(s)
}for _ = range numRange(1, 3) {
fmt.Println("bar")
}
I know this is a super old thread, but it's still the first thing that comes up if someone googles "golang range int" and I imagine a lot of people might be coming to the language from perl, python or ruby where you can do"for (1..5) { #stuff }""for _ in range(5)"and"5.times {}"respectively, and are looking for an easy and clean-looking way to do it in Go.
"for (1..5) { #stuff }""for _ in range(5)"and"5.times {}"
respectively, and are looking for an easy and clean-looking way to do it in Go.
Sublime auto generates a standard for loop so there is only the upper bound to fill in. :-)