func process(data []int) { // some processing here } func main() { var wg syncutil.Group data := []int{1, 2, 3, 4, 5} for _, item := range data { wg.Go(func() { process(item) }) } // wait for all goroutines to complete wg.Wait() }
func main() { var wg syncutil.Group for i := 0; i < 10; i++ { wg.Go(func() { // some long-running task time.Sleep(time.Second) }) } // wait for all goroutines to complete or until timeout occurs err := wg.WaitTimeout(time.Duration(5 * time.Second)) if err != nil { fmt.Println("Some goroutines did not complete within the timeout.") } }This example shows how to use the `WaitTimeout` method of the `Group` type to wait for all goroutines to complete within a specified timeout duration. In this case, the `Go` method is used to execute ten goroutines that each sleep for one second. The `WaitTimeout` method is then called with a timeout duration of five seconds. If any of the goroutines do not complete within this duration, an error is returned. In conclusion, the go4.org.syncutil package provides synchronization utilities for coordinating concurrent operations in a safe and efficient manner. The Group type is a key feature of this package, allowing multiple goroutines to be executed in parallel and then waited for before returning.