Catching return values from goroutines

Why is it not possible to fetch a return value from a goroutine assigning it to a variable? Run goroutine (asynchronously) and fetch return value from function are essentially contradictory actions. When you say go you mean “do it asynchronously” or even simpler: “Go on! Don’t wait for the function execution be finished”. But when … Read more

How to stop a goroutine

Typically, you pass the goroutine a (possibly separate) signal channel. That signal channel is used to push a value into when you want the goroutine to stop. The goroutine polls that channel regularly. As soon as it detects a signal, it quits. quit := make(chan bool) go func() { for { select { case <- … Read more

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

You can use sync.WaitGroup. Quoting the linked example: package main import ( “net/http” “sync” ) func main() { var wg sync.WaitGroup var urls = []string{ “http://www.golang.org/”, “http://www.google.com/”, “http://www.somestupidname.com/”, } for _, url := range urls { // Increment the WaitGroup counter. wg.Add(1) // Launch a goroutine to fetch the URL. go func(url string) { // … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)