Go Routine

Reference: Go by examplearrow-up-right, A Tour of Goarrow-up-right, Go Programming by Examplearrow-up-right

What is Go routines?

  • Lightweight thread managed by Go runtime

  • When calling a function using the "go" keyword in Go, runtime executes a new goroutine

  • goroutine executes function routines asynchronously, so it's used to execute multiple codes concurrently

Why Go routine?

  • Created to implement asynchronous concurrent processing much lighter than OS Thread, basically managed by Go runtime itself

    • Multiple goroutines, which are work units managed on Go 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 runtime manages goroutines and makes it easy to communicate between goroutines through Go channels

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)
} 

Last updated