Make go code run in the background

Given the following code:

var sum int
for _, num := range slice {
    sum += num
}
fmt.Println(sum)

To run it in the background, do three things:

  • Wrap the entire function in an anonymous function (func() {}), or move it to a function of its own.

  • Encapsulate all scoped variables as parameters to the new function so the goroutine can store the values in its own stack instead of some shared memory.

  • Add the keyword go to the function call. This will start the functions as a goroutine.

go func(slice []int) {
    var sum int
    for _, num := range slice {
        sum += num
    }
    fmt.Println(sum)
}(slice)

References