Goroutines are a fundamental feature of Go programming, enabling concurrent execution of functions. They are lightweight threads managed by the Go runtime, allowing developers to write efficient, concurrent programs with ease.
Goroutines are functions or methods that run concurrently with other functions or methods. They are much lighter than traditional threads, with minimal overhead and quick startup times. This makes them ideal for concurrent programming in Go.
To create a goroutine, simply use the go
keyword followed by a function call:
go functionName()
This launches the function as a goroutine, allowing it to run concurrently with the rest of the program.
package main
import (
"fmt"
"time"
)
func sayHello() {
fmt.Println("Hello from goroutine!")
}
func main() {
go sayHello()
time.Sleep(time.Second)
fmt.Println("Main function")
}
In this example, sayHello()
runs concurrently with the main function. The time.Sleep()
ensures the program doesn't exit before the goroutine has a chance to execute.
Goroutines often work in tandem with Go channels for communication and synchronization between concurrent processes. Channels provide a way for goroutines to safely share data and coordinate their execution.
package main
import (
"fmt"
)
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // Send sum to channel
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // Receive from channel
fmt.Println(x, y, x+y)
}
This example demonstrates how goroutines can work with channels to perform concurrent computations and share results.
Goroutines are a powerful feature in Go, enabling efficient concurrent programming. By understanding their usage and best practices, developers can create scalable and performant applications. Remember to use them judiciously and in conjunction with other Go concurrency primitives for optimal results.