Reference: Go by example, A Tour of Go, Go Programming by Example
- Lightweight thread managed by Go runtime
- When calling a function using the "go" keyword in Go, runtime executes a new
goroutine goroutineexecutes function routines asynchronously, so it's used to execute multiple codes concurrently
- Created to implement asynchronous concurrent processing much lighter than OS Thread, basically managed by Go runtime itself
- Multiple
goroutines, which are work units managed onGo runtime, are executed as one OS Thread- That is, goroutines don't correspond 1:1 with OS Threads, but use much fewer OS Threads through multiplexing
- In terms of memory, while OS Thread has a stack of 1MB, goroutine has a much smaller stack of a few KB
Go runtimemanages goroutines and makes it easy to communicate between goroutines through Go channels
- Multiple
ex)
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 10; i++ {
fmt.Println(s, "---->", i)
}
}
func main() {
// 1. The Synchronous way
say("Sync")
// 2. Go routines
go say("Async 1")
go say("Async 2")
go say("Async 3")
// Sleep for 3 sec
time.Sleep(time.Second * 3)
}