mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 13:16:02 +03:00
add a fanin pattern codes
This commit is contained in:
parent
eaa6f05ffd
commit
6a74cbe51b
@ -4,12 +4,11 @@
|
|||||||
|
|
||||||
目前包括了**39种Go中常用的、面向工程化和最佳实践的模式/套路**,自然也包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,甚至每个模式下的例子,改一下名字,稍微再增加几行代码就可以直接用在项目和工程中了。
|
目前包括了**39种Go中常用的、面向工程化和最佳实践的模式/套路**,自然也包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,甚至每个模式下的例子,改一下名字,稍微再增加几行代码就可以直接用在项目和工程中了。
|
||||||
|
|
||||||
|
|
||||||
每一种设计模式都有其特定的应用场景和要解决的问题,了解模式的关键点就在于弄清这些目标场景和问题,千万不要纠结于:为什么这个模式叫这个名字,这个模式为啥要这样用?
|
每一种设计模式都有其特定的应用场景和要解决的问题,了解模式的关键点就在于弄清这些目标场景和问题,千万不要纠结于:为什么这个模式叫这个名字,这个模式为啥要这样用?
|
||||||
|
|
||||||
**这些模式不是你总结的,也不是我的总结的,如果是你的写的,你可以按照自己的喜欢的感觉给这些套路取名字,让别人去费劲想。**
|
**这些模式不是你总结的,也不是我的总结的,如果是你的写的,你可以按照自己的喜欢的感觉给这些套路取名字,让别人去费劲想。**
|
||||||
|
|
||||||
## 姿势 Ways
|
## 了解一下姿势 Ways
|
||||||
|
|
||||||
+ 所谓模式就是套路,如功夫,招有定式
|
+ 所谓模式就是套路,如功夫,招有定式
|
||||||
+ 学习模式,就是学习套路,弄清楚套路要解决的目标场景,这很重要.
|
+ 学习模式,就是学习套路,弄清楚套路要解决的目标场景,这很重要.
|
||||||
@ -26,7 +25,7 @@
|
|||||||
+ [x] [迭代器模式(Iterator)](./behavior/04_iterator)
|
+ [x] [迭代器模式(Iterator)](./behavior/04_iterator)
|
||||||
+ [x] [模板方法模式(Template Method)](./behavior/05_template_method)
|
+ [x] [模板方法模式(Template Method)](./behavior/05_template_method)
|
||||||
+ [x] [策略模式(Strategy)](./behavior/12_strategy)
|
+ [x] [策略模式(Strategy)](./behavior/12_strategy)
|
||||||
+ [ ] [WIP][状态模式(State)](./behavior/behavior16_state)
|
+ [x] [状态模式(State)](./behavior/behavior16_state)
|
||||||
+ [x] [访问者模式(Visitor)](./behavior/07_visitor)
|
+ [x] [访问者模式(Visitor)](./behavior/07_visitor)
|
||||||
+ [x] [解释器模式(Interpreter)](./behavior/08_interpreter)
|
+ [x] [解释器模式(Interpreter)](./behavior/08_interpreter)
|
||||||
+ [x] [职责链模式(Chain of Responsibility)](./behavior/06_chain_of_responsibility)
|
+ [x] [职责链模式(Chain of Responsibility)](./behavior/06_chain_of_responsibility)
|
||||||
@ -76,7 +75,6 @@
|
|||||||
[go-resiliency](https://github.com/eapache/go-resiliency) | [Behavioral](https://github.com/AlexanderGrom/go-patterns/tree/master/Behavioral)|v|
|
[go-resiliency](https://github.com/eapache/go-resiliency) | [Behavioral](https://github.com/AlexanderGrom/go-patterns/tree/master/Behavioral)|v|
|
||||||
| [go-patterns](https://github.com/sevenelevenlee/go-patterns) | [go_design_pattern](https://github.com/monochromegane/go_design_pattern)|p|
|
| [go-patterns](https://github.com/sevenelevenlee/go-patterns) | [go_design_pattern](https://github.com/monochromegane/go_design_pattern)|p|
|
||||||
|
|
||||||
|
|
||||||
## 更多 More
|
## 更多 More
|
||||||
|
|
||||||
需要重新温习下Go基础?看这里
|
需要重新温习下Go基础?看这里
|
||||||
|
38
gomore/04_fan_in/fan_in.go
Normal file
38
gomore/04_fan_in/fan_in.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package fanin
|
||||||
|
|
||||||
|
func generatePipeline(numbers []int) <-chan int {
|
||||||
|
out := make(chan int)
|
||||||
|
go func() {
|
||||||
|
for _, n := range numbers {
|
||||||
|
out <- n
|
||||||
|
}
|
||||||
|
close(out)
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func squareNumber(in <-chan int) <-chan int {
|
||||||
|
out := make(chan int)
|
||||||
|
go func() {
|
||||||
|
for n := range in {
|
||||||
|
out <- n * n
|
||||||
|
}
|
||||||
|
close(out)
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func fanIn(input1, input2 <-chan int) <-chan int {
|
||||||
|
c := make(chan int)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case s := <-input1:
|
||||||
|
c <- s
|
||||||
|
case s := <-input2:
|
||||||
|
c <- s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return c
|
||||||
|
}
|
26
gomore/04_fan_in/fan_in_test.go
Normal file
26
gomore/04_fan_in/fan_in_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package fanin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFanIn(T *testing.T) {
|
||||||
|
randomNumbers := []int{13, 44, 56, 99, 9, 45, 67, 90, 78, 23}
|
||||||
|
// generate the common channel with inputs
|
||||||
|
inputChan := generatePipeline(randomNumbers)
|
||||||
|
|
||||||
|
// Fan-out to 2 Go-routine
|
||||||
|
c1 := squareNumber(inputChan)
|
||||||
|
c2 := squareNumber(inputChan)
|
||||||
|
|
||||||
|
// Fan-in the resulting squared numbers
|
||||||
|
c := fanIn(c1, c2)
|
||||||
|
sum := 0
|
||||||
|
|
||||||
|
// Do the summation
|
||||||
|
for i := 0; i < len(randomNumbers); i++ {
|
||||||
|
sum += <-c
|
||||||
|
}
|
||||||
|
fmt.Printf("Total Sum of Squares: %d", sum)
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
fanout
|
package fanout
|
||||||
|
|
||||||
import concurrency
|
import "concurrency"
|
||||||
|
|
||||||
type taggingDispatcher struct {
|
type taggingDispatcher struct {
|
||||||
Address string
|
Address string
|
||||||
|
Loading…
Reference in New Issue
Block a user