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")
}
direct : 0
direct : 1
direct : 2
going
goroutine : 0
goroutine : 1
goroutine : 2
done
Explanation:
-
package main
: Indicates that this Go file belongs to the main executable package. -
import (...)
: Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations. -
func f(from string) { ... }
: Defines a functionf
that takes a string parameterfrom
and prints a message three times with the given prefix. -
func main() { ... }
: The main function, where the execution of the program begins. -
f("direct")
: Calls the functionf
in the main goroutine, printing a message directly. -
go f("goroutine")
: Launches a new goroutine to execute the functionf("goroutine")
concurrently, allowing it to run independently of the main program. -
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. -
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. -
fmt.Println("done")
: Prints "done" after the goroutines have had enough time to execute, indicating the end of the program.