Home  >  Article  >  Backend Development  >  When to Use Parameters vs. Closures in Go Anonymous Functions?

When to Use Parameters vs. Closures in Go Anonymous Functions?

Barbara Streisand
Barbara StreisandOriginal
2024-11-19 05:00:02881browse

When to Use Parameters vs. Closures in Go Anonymous Functions?

Passing Parameters to Function Closures

In Go, anonymous functions with passed parameters can behave differently from closures. This article delves into the distinction and its implications.

Closure Example:

func main() {
  done := make(chan bool, 1)
  go func() {
    time.Sleep(50 * time.Millisecond)
    done <- true
  }()
  <-done
}

In this closure, the anonymous function has no parameters and instead accesses the done channel present in the surrounding scope.

Parameter Example:

func main() {
  done := make(chan bool, 1)
  go func(c chan bool) {
    time.Sleep(50 * time.Millisecond)
    c <- true
  }(done)
  <-done
}

In this case, the anonymous function takes a parameter, c, which is a channel, and interacts with it directly.

When to Use Parameters

Passing parameters to an anonymous function is mainly beneficial in scenarios where sharing variables can lead to unexpected behavior. Consider the following example:

for i := 0; i < 3; i++ {
    go func() {
        fmt.Println(i)
    }() // Closure
}

Here, the anonymous function is a closure, i.e., it refers to the i variable defined in the for loop. As all goroutines share the same i variable, they will print "3" (the final value of i after the loop completes), even though we may expect them to print "0", "1", and "2".

To rectify this issue, we can pass i as a parameter:

for i := 0; i < 3; i++ {
    go func(v int) {
        fmt.Println(v)
    }(i) // Parameter
}

Now, each goroutine receives a copy of the i variable when the function is called, resulting in the correct output: "0", "1", and "2".

Conclusion

While closures allow for convenient access to variables within the surrounding scope, passing parameters provides more control and ensures predictable behavior when dealing with shared variables, particularly in concurrent scenarios.

The above is the detailed content of When to Use Parameters vs. Closures in Go Anonymous Functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn