Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (40 loc) · 1.73 KB
/
Copy pathmain.go
File metadata and controls
56 lines (40 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Goroutines are lightweight threads managed by the Go runtime. They allow concurrent execution of functions, making it easier to write concurrent programs. In this example, we will demonstrate how to create and run goroutines in Go. Go provides the `go` keyword to start a new goroutine. When you call a function with the `go` keyword, it runs concurrently with the calling function.
// package main
// import (
// "fmt"
// // "time"
// )
// func task(id int) {
// fmt.Printf("Task %d is running\n", id)
// }
// func main() {
// for i := 1; i <= 5; i++ {
// go task(i) // Start a new goroutine for each task
// }
// // time.Sleep(1 * time.Second) // Sleep for a second to allow goroutines to finish (not a recommended practice)
// }
// Go Concurrency vs Parallelism := Concurrency is the ability of a program to manage multiple tasks at the same time, while parallelism is the ability to execute multiple tasks simultaneously. In Go, goroutines provide concurrency, allowing you to write programs that can handle multiple tasks concurrently without blocking the main execution flow.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
go func() {
time.Sleep(300 * time.Millisecond)
fmt.Println("Goroutine 1 finished", time.Since(start))
}()
go func() {
time.Sleep(150 * time.Millisecond)
fmt.Println("Goroutine 2 finished", time.Since(start))
}()
fmt.Println("Main function finished", time.Since(start))
fmt.Println("Waiting...", time.Since(start))
time.Sleep(100 * time.Millisecond)
fmt.Println("Waiting...", time.Since(start))
time.Sleep(100 * time.Millisecond)
fmt.Println("Waiting...", time.Since(start))
time.Sleep(500 * time.Millisecond)
fmt.Println("All goroutines finished", time.Since(start))
}