Skip to content

Commit

Permalink
Range over int (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan authored Jun 22, 2024
1 parent 56f0550 commit 9078cc5
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 19 deletions.
6 changes: 3 additions & 3 deletions csv/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (w *Writer) WriteFields(fields any) error {
return fmt.Errorf("can not get fieldnames from zero field struct")
}

for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
if f := v.Type().Field(i); f.IsExported() {
tag, _ := v.Type().Field(i).Tag.Lookup("csv")
w.fields = append(w.fields, field{v.Type().Field(i).Name, tag})
Expand Down Expand Up @@ -147,7 +147,7 @@ func (w *Writer) Write(record any) error {
for i, field := range w.fields {
var val reflect.Value
var found bool
for i := 0; i < v.NumField(); i++ {
for i := range v.NumField() {
if tag, ok := v.Type().Field(i).Tag.Lookup("csv"); ok && tag == field.tag {
val = v.FieldByName(field.name)
found = true
Expand Down Expand Up @@ -199,7 +199,7 @@ func (w *Writer) WriteAll(records any) error {
}

v := reflect.ValueOf(records)
for i := 0; i < v.Len(); i++ {
for i := range v.Len() {
if err := w.Write(v.Index(i).Interface()); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion executor/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Execute[T any](argMethod, fnMethod Method, limit int, args []T, fn ...func(
rand.Shuffle(count, func(i, j int) { clone[i], clone[j] = clone[j], clone[i] })
}

for i := 0; i < count; i++ {
for i := range count {
ctx := argContext(len(fn), clone[i])
workers.RunSlice(limit, fn, func(_ int, fn func(T) (any, error)) {
ctx.runFn(fn, result, lasterr)
Expand Down
2 changes: 1 addition & 1 deletion loadbalance/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func WeightedRandom[E any](items ...Weighted[E]) (LoadBalancer[E], error) {
func (r *random[E]) load() {
length := len(r.items)
var s []int
for i := 0; i < length; i++ {
for i := range length {
s = append(s, i)
}
rand.Shuffle(length, func(i, j int) { s[i], s[j] = s[j], s[i] })
Expand Down
4 changes: 2 additions & 2 deletions loadbalance/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRandom(t *testing.T) {
t.Error(err)
} else {
var res []string
for i := 0; i < 6; i++ {
for range 6 {
res = append(res, loadbalancer.Next())
}
sort.Strings(res)
Expand All @@ -32,7 +32,7 @@ func TestRandom(t *testing.T) {
t.Error(err)
} else {
var res []string
for i := 0; i < 8; i++ {
for range 8 {
res = append(res, loadbalancer.Next())
}
sort.Strings(res)
Expand Down
4 changes: 2 additions & 2 deletions loadbalance/roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRoundRobin(t *testing.T) {
t.Error(err)
} else {
var res []string
for i := 0; i < 6; i++ {
for range 6 {
res = append(res, loadbalancer.Next())
}
if expect := []string{a, b, c, a, b, c}; !reflect.DeepEqual(res, expect) {
Expand All @@ -30,7 +30,7 @@ func TestRoundRobin(t *testing.T) {
t.Error(err)
} else {
var res []string
for i := 0; i < 8; i++ {
for range 8 {
res = append(res, loadbalancer.Next())
}
if expect := []string{a, a, b, c, a, a, b, c}; !reflect.DeepEqual(res, expect) {
Expand Down
8 changes: 3 additions & 5 deletions progressbar/progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ func TestProgessBar(t *testing.T) {

pb := New(15).SetRefresh(4 * time.Second)
pb.Start()
for i := int64(0); i < pb.total; i++ {
//log.Print(i)
for range pb.total {
pb.Add(1)
time.Sleep(time.Second)
}
pb.Done()

pb = New(10).SetRefresh(500 * time.Millisecond)
pb.Start()
for i := int64(0); i < pb.total; i++ {
//log.Print(i)
for range pb.total {
pb.Add(1)
time.Sleep(time.Second)
}
Expand All @@ -45,7 +43,7 @@ func TestCancel(t *testing.T) {
pb.Cancel()
}()
go func() {
for i := int64(0); i < pb.total; i++ {
for range pb.total {
pb.Add(1)
time.Sleep(time.Second)
}
Expand Down
2 changes: 1 addition & 1 deletion retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ErrNoMoreRetry(err string) error { return errorNoMoreRetry(err) }
// Do keeps retrying the function until no error is returned.
func Do(fn func() error, attempts, delay int) error {
var errs []error
for i := 0; i < attempts; i++ {
for i := range attempts {
err := fn()
if err == nil {
return nil
Expand Down
8 changes: 4 additions & 4 deletions workers/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func RunSlice[E any](limit int, s []E, f func(int, E)) {
}

c := make(chan struct{}, limit)
for i := 0; i < len(s); i++ {
for i := range len(s) {
c <- struct{}{}
go func(index int, value E) {
defer func() { <-c }()
f(index, value)
}(i, s[i])
}

for i := 0; i < limit; i++ {
for range limit {
c <- struct{}{}
}
close(c)
Expand All @@ -102,7 +102,7 @@ func RunMap[K comparable, V any](limit int, m map[K]V, f func(K, V)) {
}(k, v)
}

for i := 0; i < limit; i++ {
for range limit {
c <- struct{}{}
}
close(c)
Expand All @@ -129,7 +129,7 @@ func RunRange[Int Integer](limit int, start, end Int, f func(Int)) {
}(i)
}

for i := 0; i < limit; i++ {
for range limit {
c <- struct{}{}
}
close(c)
Expand Down

0 comments on commit 9078cc5

Please sign in to comment.