Code with Go
Ask or search…
⌃K
Links

Goroutines

This Go code demonstrates the basics of goroutines and how they interact with the main program. It illustrates the basics of goroutines, showing how they can run concurrently with the main program and the use of time.Sleep for synchronization.
Let's break it down with inline comments:
package main
​
import (
"fmt"
"time"
)
​
// Function to print messages three times
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
​
func main() {
// Calling the function in the main goroutine
f("direct")
​
// Launching a new goroutine to execute the function concurrently
go f("goroutine")
​
// Using an anonymous function in a goroutine with a parameter
go func(msg string) {
fmt.Println(msg)
}("going")
​
// Giving some time for the goroutines to finish before exiting the program
time.Sleep(time.Second)
​
// Print "done" after all goroutines have completed
fmt.Println("done")
}

Output

direct : 0
direct : 1
direct : 2
going
goroutine : 0
goroutine : 1
goroutine : 2
done
Explanation:
  1. 1.
    package main: Indicates that this Go file belongs to the main executable package.
  2. 2.
    import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.
  3. 3.
    func f(from string) { ... }: Defines a function f that takes a string parameter from and prints a message three times with the given prefix.
  4. 4.
    func main() { ... }: The main function, where the execution of the program begins.
  5. 5.
    f("direct"): Calls the function f in the main goroutine, printing a message directly.
  6. 6.
    go f("goroutine"): Launches a new goroutine to execute the function f("goroutine") concurrently, allowing it to run independently of the main program.
  7. 7.
    go func(msg string) { ... }("going"): Creates an anonymous function and launches it as a goroutine with a parameter. This demonstrates how to use goroutines with inline function definitions.
  8. 8.
    time.Sleep(time.Second): Introduces a delay of one second, giving the goroutines some time to complete before the program exits. This is a simple way to synchronize the main goroutine with the others.
  9. 9.
    fmt.Println("done"): Prints "done" after the goroutines have had enough time to execute, indicating the end of the program.
Last modified 1mo ago