How to wait for all goroutines to finish without using time.Sleep?


This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the file is printed out).

How do I avoid using the sleep method to keep the main method from exiting? I have problems wrapping my head around channels (I assume that’s what it takes, to synchronize the results) so any help is appreciated!

package main

import (

func eachFile(extension string, callback func(file string)) {
    exeDir := filepath.Dir(os.Args[0])
    files, _ := ioutil.ReadDir(exeDir)
    for _, f := range files {
            fileName := f.Name()
            if extension == path.Ext(fileName) {
                go callback(fileName)

func main() {
    maxProcs := runtime.NumCPU()

    eachFile(".xml", func(fileName string) {
                // Custom logic goes in here

    // This is what i want to get rid of
    time.Sleep(100 * time.Millisecond)


You can use sync.WaitGroup. Quoting the linked example:

package main

import (

func main() {
        var wg sync.WaitGroup
        var urls = []string{
        for _, url := range urls {
                // Increment the WaitGroup counter.
                // Launch a goroutine to fetch the URL.
                go func(url string) {
                        // Decrement the counter when the goroutine completes.
                        defer wg.Done()
                        // Fetch the URL.
        // Wait for all HTTP fetches to complete.

Answered By – zzzz

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.