Sobes.tech
Senior

1. Mida see näitab, kuidas parandada, kuidas muuta voogude kaitstuks (waitGroup) 2. Korrata duplikaadid loendis

sobes.tech AI

Vastus AI-lt

  1. What will be displayed, how to fix, how to make thread-safe (waitGroup)

Without specific code, it's difficult to say exactly, but a common mistake with sync.WaitGroup in Go is incorrect usage of the methods Add, Done, and Wait, which can lead to panics or deadlocks.

Correct usage example:

var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    // work
}()
wg.Wait()

It's important to call Add before starting the goroutine to avoid race conditions.

To make it thread-safe, you need to properly synchronize access to shared data, for example, using mutexes or channels.


  1. Remove duplicates from a slice

Example function to remove duplicates from a slice of strings:

func removeDuplicates(slice []string) []string {
    seen := make(map[string]struct{})
    result := []string{}
    for _, v := range slice {
        if _, ok := seen[v]; !ok {
            seen[v] = struct{}{}
            result = append(result, v)
        }
    }
    return result
}

For other types, a similar approach can be used with a map to track unique elements.