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
func gen(nums ...int) <-chan int { | |
out := make(chan int) | |
go func() { | |
for _, n := range nums { | |
out <- n | |
} | |
close(out) | |
}() | |
return out | |
} |
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
func sq(in <-chan int) <-chan int { | |
out := make(chan int) | |
go func() { | |
for n := range in { | |
out <- n * n | |
} | |
close(out) | |
}() | |
return out | |
} |
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
func main() { | |
in := gen(2, 3) | |
// Distribute the sq work across two goroutines that both read from in. | |
c1 := sq(in) | |
c2 := sq(in) | |
// Consume the merged output from c1 and c2. | |
for n := range merge(c1, c2) { | |
fmt.Println(n) // 4 then 9, or 9 then 4 |
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
func merge(cs ...<-chan int) <-chan int { | |
var wg sync.WaitGroup | |
out := make(chan int) | |
// Start an output goroutine for each input channel in cs. output | |
// copies values from c to out until c is closed, then calls wg.Done. | |
output := func(c <-chan int) { | |
for n := range c { | |
out <- n | |
} |
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
func hello(done chan bool) { | |
fmt.Println("Hello world goroutine") | |
done <- true | |
} | |
func main() { | |
done := make(chan bool) | |
go hello(done) | |
<-done | |
fmt.Println("main function") | |
} |
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
// From https://golangbot.com/channels/ | |
func calcSquares(number int, squareop chan int) { | |
sum := 0 | |
for number != 0 { | |
digit := number % 10 | |
sum += digit * digit | |
number /= 10 | |
} | |
squareop <- sum |
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
timeout := make(chan bool, 1) | |
go func() { | |
time.Sleep(1 * time.Second) | |
timeout <- true | |
}() | |
select { | |
case <-ch: | |
// a read from ch has occurred | |
case <-timeout: |
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
func Query(conns []Conn, query string) Result { | |
ch := make(chan Result) | |
for _, conn := range conns { | |
go func(c Conn) { | |
select { | |
case ch <- c.DoQuery(query): | |
default: | |
} | |
}(conn) | |
} |
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
package main | |
import "fmt" | |
func main() { | |
channel := make(chan int) | |
done := make(chan bool) | |
start := 0 | |
go playerOne(channel, done) |
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
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: my-nginx | |
spec: | |
selector: | |
matchLabels: | |
run: my-nginx | |
replicas: 2 | |
template: |
OlderNewer