I want to get a slice in Go which looks like this:
[100, 200, 300, 400, 500]
In Python I would do this:
l = range(100, 600, 100)
I know I can do this in Go:
l := []int{}
for i:=100; i<600; i+=100{
l = append(l, i)
}
but isn't there anything simpler to create this slice?
Do it the same way Python does:
func pyrange(start, end, step int) []int {
// TODO: Error checking to make sure parameters are all valid,
// else you could get divide by zero in make and other errors.
rtn := make([]int, 0, (end-start)/step)
for i := start; i < end; i += step {
rtn = append(rtn, i)
}
return rtn
}
With a function.
Obviously only worth the time if you need to do this often. By default Go does not include a function like this, so you need to write your own (or find a third party library) if you want one.