Concurrency Basics
Goroutine Handoff
A goroutine can send a result back to the main flow through a channel.
Goroutine Handoff
goroutine_handoff.go
package main
import "fmt"
func main() {
var label =
done := make(chan string)
go func() {
done <- "worker:" + label
}()
message := <-done
fmt.Println("label=", label)
fmt.Println("message=", message)
fmt.Println("received=", true)
}
package main
import "fmt"
func main() {
var label =
done := make(chan string)
go func() {
done <- "worker:" + label
}()
message := <-done
fmt.Println("label=", label)
fmt.Println("message=", message)
fmt.Println("received=", true)
}
package main
import "fmt"
func main() {
var label =
done := make(chan string)
go func() {
done <- "worker:" + label
}()
message := <-done
fmt.Println("label=", label)
fmt.Println("message=", message)
fmt.Println("received=", true)
}
goroutine
A goroutine runs a function concurrently, and a channel can make the handoff deterministic.