Last active
December 13, 2018 12:07
-
-
Save axiaoxin/4f51aca51048dbefb3f3ff985ea460e5 to your computer and use it in GitHub Desktop.
Go Patterns
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 faninout | |
import ( | |
"fmt" | |
) | |
func main() { | |
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) | |
} | |
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 | |
} |
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 future | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
type data struct { | |
Body []byte | |
Error error | |
} | |
func futureData(url string) <-chan data { | |
c := make(chan data, 1) | |
go func() { | |
var body []byte | |
var err error | |
resp, err := http.Get(url) | |
defer resp.Body.Close() | |
body, err = ioutil.ReadAll(resp.Body) | |
c <- data{Body: body, Error: err} | |
}() | |
return c | |
} | |
func main() { | |
future := futureData("http://test.future.com") | |
// do many other things | |
body := <-future | |
fmt.Printf("response: %#v", string(body.Body)) | |
fmt.Printf("error: %#v", body.Error) | |
} |
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" | |
// Generator func which produces data which might be computationally expensive. | |
func fib(n int) chan int { | |
c := make(chan int) | |
go func() { | |
for i, j:= 0, 1; i < n ; i, j = i+j,i { | |
c <- i | |
} | |
close(c) | |
}() | |
return c | |
} | |
func main() { | |
// fib returns the fibonacci numbers lesser than 1000 | |
for i := range fib(1000) { | |
// Consumer which consumes the data produced by the generator, which further does some extra computations | |
v := i*i | |
fmt.Println(v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment