Senior
Do you have an idea about the difference between a thread and a process?
sobes.tech AI
Answer from AI
A process is an independent execution unit with its own address space, resources, and state. A thread (goroutine in Go) is a lighter-weight unit within a process that shares the process's memory and resources but has its own stack and execution context.
In Go, a goroutine is a lightweight thread managed by the Go runtime, allowing thousands of concurrent tasks with minimal overhead compared to system threads.
Main differences:
- A process has a separate address space, while threads within a process share memory.
- Creating a process is more resource-intensive than creating a thread.
- In Go, a goroutine is a thread managed at the language level, not by the OS.
Example of creating a goroutine in Go:
func sayHello() {
fmt.Println("Hello from goroutine")
}
func main() {
go sayHello() // runs the function in a separate goroutine
time.Sleep(time.Second) // wait to let the goroutine finish
}