-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel01.go
83 lines (70 loc) · 1.79 KB
/
level01.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"os"
"runtime"
"strings"
"time"
)
func level01() {
fmt.Println("\n* Find Environment Variables")
fmt.Println(os.Environ())
fmt.Println("\n* Get the variables separately using range")
for e := range os.Environ() {
fmt.Println(e)
}
fmt.Println("\n* Get both index and values variables separately")
for i, e := range os.Environ() {
fmt.Println(i, e)
}
fmt.Println("\n* Ignore index using underscore")
for _, e := range os.Environ() {
fmt.Println(e)
}
fmt.Println("\n* Split the values")
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
fmt.Println(pair)
}
fmt.Println("\n* Get only the environment variable names")
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
fmt.Println(pair[0])
}
fmt.Println("\n* Call a function")
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
printEnviron(pair[0])
}
fmt.Println("\n* Concurrency")
c1 := make(chan string)
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
go printEnvironConcurrent(pair[0], c1)
}
fmt.Println(<-c1) //outside the for-loop
fmt.Println("\n* Have the routines finish before main")
c2 := make(chan string)
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
go printEnvironConcurrent(pair[0], c2)
fmt.Println(<-c2) //inside the for-loop
}
fmt.Println("\n* ### Parallel run on multiple CPUs")
runtime.GOMAXPROCS(runtime.NumCPU()) //number of CPUs
c3 := make(chan string)
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
go printEnvironConcurrent(pair[0], c3)
fmt.Println(<-c3)
}
}
func printEnviron(e string) {
time.Sleep(10 * time.Millisecond)
fmt.Println(e)
}
func printEnvironConcurrent(e string, channel chan string) {
time.Sleep(10 * time.Millisecond)
fmt.Println(e)
channel <- e
}