-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefer.go
60 lines (54 loc) · 1015 Bytes
/
defer.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
package main
import (
"fmt"
"sync"
)
// func finished(){
// fmt.Println("Finished the function")
// }
// func largest(nums [] int) {
// max := 0
// for _, v := range nums{
// if max < v {
// max = v
// }
// }
// fmt.Println("The largest number is ",max)
// }
// func main(){
// nums := []int{12,3,4,5,4,3,5,45}
// largest(nums)
// }
type rect struct {
length int
width int
}
func (r rect) area(wg *sync.WaitGroup) {
defer wg.Done()
if r.length < 0 {
fmt.Printf("rect %v's length should be greater than zero\n", r)
// wg.Done()
return
}
if r.width < 0 {
fmt.Printf("rect %v's width should be greater than zero\n", r)
// wg.Done()
return
}
area := r.length * r.width
fmt.Printf("rect %v's area %d\n", r, area)
// wg.Done()
}
func main() {
var wg sync.WaitGroup
r1 := rect{-67, 89}
r2 := rect{5, -67}
r3 := rect{8, 9}
rec := []rect{r1,r2,r3}
for _ , v := range rec{
wg.Add(1)
go v.area(&wg)
}
wg.Wait()
fmt.Println("All go routines finished executing")
}