diff --git a/csv/writer.go b/csv/writer.go index 6a62a06..5d2b011 100644 --- a/csv/writer.go +++ b/csv/writer.go @@ -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}) @@ -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 @@ -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 } diff --git a/executor/method.go b/executor/method.go index 699ac72..c1a4c32 100644 --- a/executor/method.go +++ b/executor/method.go @@ -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) diff --git a/loadbalance/random.go b/loadbalance/random.go index 6615f7c..4bf53d0 100644 --- a/loadbalance/random.go +++ b/loadbalance/random.go @@ -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] }) diff --git a/loadbalance/random_test.go b/loadbalance/random_test.go index f7f0b91..8679d65 100644 --- a/loadbalance/random_test.go +++ b/loadbalance/random_test.go @@ -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) @@ -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) diff --git a/loadbalance/roundrobin_test.go b/loadbalance/roundrobin_test.go index 33a04f5..a3be176 100644 --- a/loadbalance/roundrobin_test.go +++ b/loadbalance/roundrobin_test.go @@ -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) { @@ -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) { diff --git a/progressbar/progressbar_test.go b/progressbar/progressbar_test.go index 13b5407..d6ec8e2 100644 --- a/progressbar/progressbar_test.go +++ b/progressbar/progressbar_test.go @@ -17,8 +17,7 @@ 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) } @@ -26,8 +25,7 @@ func TestProgessBar(t *testing.T) { 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) } @@ -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) } diff --git a/retry/retry.go b/retry/retry.go index 50dffde..536f9dc 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -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 diff --git a/workers/workers.go b/workers/workers.go index eeaab6f..cabf0fa 100644 --- a/workers/workers.go +++ b/workers/workers.go @@ -71,7 +71,7 @@ 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 }() @@ -79,7 +79,7 @@ func RunSlice[E any](limit int, s []E, f func(int, E)) { }(i, s[i]) } - for i := 0; i < limit; i++ { + for range limit { c <- struct{}{} } close(c) @@ -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) @@ -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)