"Future & Promises" example added. by OguzhanKazak · Pull Request #119 · tmrts/go-patterns
Added "future_promise.md" file with an explanation of the future/promise pattern in concurrent and asynchronous programming, as part of a general pattern list for reference. The file describes the basic idea behind the pattern, its benefits, and its usage in different programming languages and frameworks.
package main
import "fmt"
func add(a, b int) <-chan int {
res := make(chan int)
go func() {
res <- a + b
close(res)
}()
return res
}
func main() {
c := add(1, 2)
fmt.Println(<-c)
}
I think it would be more common to use chan directly in go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters