-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
57 lines (38 loc) · 1.3 KB
/
channels.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
package main
import (
"fmt"
"os"
"text/tabwriter"
)
func lenOfChannel() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, '.', tabwriter.AlignRight|tabwriter.Debug)
c := make(chan int, 5)
fmt.Fprintf(w,"c = make(chan int)\t len(c): %v\t cap(c): %v\t\n",len(c), cap(c))
c <- 1
fmt.Fprintf(w,"c <-1\t len(c): %v\t cap(c): %v\t\n",len(c), cap(c))
c <- 1
fmt.Fprintf(w,"c <-1\t len(c): %v\t cap(c): %v\t\n",len(c), cap(c))
c <- 1
fmt.Fprintf(w,"c <-1\t len(c): %v\t cap(c): %v\t\n",len(c), cap(c))
w.Flush()
}
// makingChannels
// buffered channels cap >= 1
// unbuffered channels cap = 0
// make takes only capacity argument when making channels - unlike slices there is no len argument
func makingChannels() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, '.', tabwriter.AlignRight|tabwriter.Debug)
var c chan int
fmt.Fprintf(w,"var c chan int\t len(c): %v\t cap(c): %v\t c is nil: %v\n",len(c), cap(c), c==nil)
c = make(chan int)
fmt.Fprintf(w,"c = make(chan int)\t len(c): %v\t cap(c): %v\t c is nil: %v\n",len(c), cap(c), c==nil)
c = make(chan int, 5)
fmt.Fprintf(w,"c = make(chan int, 5)\t len(c): %v\t cap(c): %v\t c is nil: %v\n",len(c), cap(c), c==nil)
w.Flush()
}
func main() {
// point 1 - making channels
// makingChannels()
// point 2 - length of channels
lenOfChannel()
}