Skip to content

Commit

Permalink
Channel Default Values And Best Practices
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 30, 2021
1 parent 64a8b11 commit 3af6642
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,26 @@ func(in <-chan string, out chan<- string) {
// in: This channel can only receive values of type string
// out: This channel can only send values of type string
}
```
```

-----------

## Channel Default Values And Best Practices:
- Default value for channels is `nil`.
```go
var ch chan interface{}
```
- Reading and Writing to a `nil` channel will block forever.
```go
var ch chan interface{}
<- ch
ch <- struct{}{}
```
- Closing `nil` channel will `panic`.
```go
var ch chan interface{}
close(ch)
```
- Ensure the channels are initialized first.
- Owner of channel is a **Goroutine that instantiates, writes and closes a channel**.
- Channel **utilizers only have a read-only** view into the channel.

0 comments on commit 3af6642

Please sign in to comment.