-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added part of golang course, channels
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import "fmt" | ||
import "time" | ||
import "runtime" | ||
|
||
func readChan(ch chan int) { | ||
value := <-ch | ||
fmt.Println("CHAN VALUE: ", value) | ||
} | ||
|
||
func writeChan(ch chan <- int) { | ||
for i := 1; i <= 5; i++ { | ||
ch<- i | ||
} | ||
close(ch) | ||
} | ||
|
||
func writeSelect(ch chan<- int) { | ||
ch<- 1 | ||
} | ||
|
||
func readSelect(ch, quit <-chan int) { | ||
for { | ||
select { | ||
case x := <-ch: | ||
fmt.Println("ch3 = ", x) | ||
case <-quit: | ||
fmt.Println("quit") | ||
return | ||
default: | ||
fmt.Println("default") | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Println("START MAIN") | ||
var ch chan int | ||
var ch2 chan int | ||
|
||
// Buffered and unbuffered channel | ||
ch = make(chan int, 1) | ||
// ch = make(chan int) | ||
|
||
ch<- 77 | ||
go readChan(ch) | ||
ch<- 111 | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
// for | ||
ch2 = make(chan int) | ||
go writeChan(ch2) | ||
|
||
for i := range ch2 { | ||
fmt.Println("chan i = ", i) | ||
} | ||
|
||
// select | ||
ch3 := make(chan int) | ||
quit := make(chan int) | ||
|
||
go readSelect(ch3, quit) | ||
|
||
go writeSelect(ch3) | ||
runtime.Gosched() | ||
go writeSelect(quit) | ||
|
||
|
||
fmt.Println("END MAIN") | ||
} |