AmitKumarDas / fun-with-programming

ABC - Always Be Coding
2 stars 2 forks source link

[go] goroutine snippets #47

Closed AmitKumarDas closed 2 years ago

AmitKumarDas commented 3 years ago
// https://mastanca.medium.com/my-raw-notes-on-go-best-practices-concurrency-memory-and-beyond-5d82c1dae110
// Every Go program has at least one goroutine: the main goroutine, 
// which is automatically created and started when the process begins.
//
// A goroutine is a function that is running concurrently. 
// Notice the difference between concurrence and parallelism.
// They’re not OS threads, they’re a higher level of abstraction known as coroutines. 
// Coroutines are simply concurrent subroutines that are nonpreemptive 
// i.e. (they cannot be interrupted).
// Go follows a fork-join model for launching and waiting for goroutines.
//
// Leak prevention: 
// The way to successfully mitigate this is to establish a signal between the 
// parent goroutine and its children that allows the parent to signal cancellation to its 
// children. By convention, this signal is usually a read-only
// channel named **done**. The parent goroutine passes this channel to the child 
// goroutine and then closes the channel when it wants to cancel the child goroutine.
//
// If a goroutine is responsible for creating a goroutine, it is also responsible for ensuring 
// it can stop the goroutine. For better error handling inside goroutines **create a struct 
// that wraps both possible result and possible error**. 
// Return a channel of this struct.