refactor: use range loops introduced in go 1.22

This commit is contained in:
divyam234 2024-05-20 02:54:32 +05:30
parent 7bbcbf80a2
commit c8148a2207
3 changed files with 10 additions and 10 deletions

View file

@ -163,7 +163,7 @@ func bindFlagsRecursive(flags *pflag.FlagSet, prefix string, v reflect.Value) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
for i := 0; i < t.NumField(); i++ {
for i := range t.NumField() {
field := t.Field(i)
switch field.Type.Kind() {
case reflect.Struct:

View file

@ -143,7 +143,7 @@ func (n *nonce) increment() {
func (n *nonce) add(x uint64) {
carry := uint16(0)
for i := 0; i < 8; i++ {
for i := range 8 {
digit := (*n)[i]
xDigit := byte(x)
x >>= 8

View file

@ -124,7 +124,7 @@ func getBotInfo(ctx context.Context, KV kv.KV, config *config.TGConfig, token st
return &types.BotInfo{Id: user.ID, UserName: user.Username, Token: token}, nil
}
func getTGMessagesBatch(ctx context.Context, client *telegram.Client, channel *tg.InputChannel, parts []schemas.Part, userID string, index int,
func getTGMessagesBatch(ctx context.Context, client *telegram.Client, channel *tg.InputChannel, parts []schemas.Part, index int,
results chan<- batchResult, errors chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
@ -172,10 +172,10 @@ func getTGMessages(ctx context.Context, client *telegram.Client, parts []schemas
errors := make(chan error, batchCount)
for i := 0; i < batchCount; i++ {
for i := range batchCount {
wg.Add(1)
splitParts := parts[i*batchSize : min((i+1)*batchSize, len(parts))]
go getTGMessagesBatch(ctx, client, channel, splitParts, userID, i, results, errors, &wg)
go getTGMessagesBatch(ctx, client, channel, splitParts, i, results, errors, &wg)
}
wg.Wait()
@ -188,19 +188,19 @@ func getTGMessages(ctx context.Context, client *telegram.Client, parts []schemas
}
}
channelResult := []batchResult{}
batchResults := []batchResult{}
for result := range results {
channelResult = append(channelResult, result)
batchResults = append(batchResults, result)
}
sort.Slice(channelResult, func(i, j int) bool {
return channelResult[i].Index < channelResult[j].Index
sort.Slice(batchResults, func(i, j int) bool {
return batchResults[i].Index < batchResults[j].Index
})
allMessages := []tg.MessageClass{}
for _, result := range channelResult {
for _, result := range batchResults {
allMessages = append(allMessages, result.Messages.GetMessages()...)
}