Limiting the maximum amount of goroutines run simultaneously Here is my code which helps me to contol the maximum amount of goroutines run simultaneously.
In example below maximum is 5. 

var wg sync.WaitGroup
// Create bufferized channel with size 5
goroutines := make(chan struct{}, 5)
// Read data from input channel
for data := range input {
    // 1 struct{}{} - 1 goroutine
    goroutines &lt;- struct{}{}
    wg.Add(1)
    go func(data string, goroutines &lt;-chan struct{}, results chan&lt;- result, wg *sync.WaitGroup) {
        // Process data
        results &lt;- process(data)
        // Read from "goroutines" channel to free space for new goroutine
        &lt;-goroutines
        wg.Done()
    }(data, goroutines, results, &amp;wg)
}

wg.Wait()
close(goroutines)


Is there a better way to control the maximum amount of goroutines?How can I improve my code?
You can use the Golang Concurrency Manager goccm to limit the maximum number of goroutines to run concurrently:

Example:

package main

import (
    "fmt"
    "goccm"
    "time"
)

func main()  {
    // Limit 3 goroutines to run concurrently.
    c := goccm.New(3)

    for i := 1; i &lt;= 10; i++ {

        // This function have to call before any goroutine
        c.Wait()

        go func(i int) {
            fmt.Printf("Job %d is running\n", i)
            time.Sleep(2 * time.Second)

            // This function have to when a goroutine has finished
            // Or you can use `defer c.Done()` at the top of goroutine.
            c.Done()
        }(i)
    }

    // This function have to call to ensure all goroutines have finished 
    // after close the main program.
    c.WaitAllDone()
}