

Go Thread - Goroutines
A goroutine is a lightweight thread managed by the Go runtime.Goroutines are functions or methods that run concurrently with other functions or methods. Goroutines can be thought of as lightweight threads. The cost of creating a Goroutine is tiny when compared to a thread. Hence it's common for Go applications to have thousands of Goroutines running concurrently.
Goroutine Syntax
go f(x, y, z)
starts a new goroutine running
f(x, y, z)
The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.
Goroutines run in the same address space, so access to shared memory must be synchronized. The sync package provides useful primitives, although you won't need them much in Go as there are other primitives. (See the next slide.)
Example of Go Goroutines
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
Difference between Goroutines and Thread
GoroutineGoroutine is method/function which can be executed independently along with other goroutines. Every concurrent activity in Go language is generally terms as gorountine.
Thread
Thread is a lightweight process. It can be treated as a unit to execute a piece of code. Operating system manages the thread.
Sr. No. | Key | Goroutine | Thread |
---|---|---|---|
1 | Managed By | Goroutine methods are managed by golang runtime. | Thread are managed by operating systems. |
2 | Hardware dependency | Goroutine are independent to hardware. | Thread are dependent on hardware. |
3 | Communication Medium | Goroutines uses channels as communication medium. | Thread have no easy communication medium. |
4 | Latency | Goroutines can commuicate with other routines with low latency. | Thread as have no communication medium, communicate with other thread with high latency. |
5 | ID | Goroutine does not have any thread local storage and no unique id. | Thread have thread local storage and have unique id. |
6 | Scheduling | Goroutines are co-operatively scheduled. | Threads are preemptively scheduled. |
7 | Startup | Goroutines have faster startup time. | Threads have slower startup time. |
8 | Stack | Goroutines have growable segmented stacks. | Threads do not have growable segmented stacks. |
Conclusion
In this page (written and validated by A. Gawali) you learned about Go Thread - Goroutines . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Log and Logger.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.
Share On: |
Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.