Last active
August 18, 2017 06:13
-
-
Save stevenferrer/9b2eeac3eed3f7667e8976f399d0b8ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"time" | |
) | |
func main() { | |
ints := []int{} | |
n := 10 | |
for x := 0; x < n; x++ { | |
ints = append(ints, x+1) | |
} | |
intsChan := makePipeline(ints) | |
start := time.Now() | |
for cnt := 0; cnt < len(ints); cnt++ { | |
i := <-intsChan | |
fmt.Print(i, " ") | |
} | |
fmt.Println("Elapsed: ", time.Since(start)) | |
} | |
func makePipeline(ints []int) <-chan int { | |
intsChan := make(chan int) | |
go func() { | |
for _, i := range ints { | |
go func(i int) { | |
intsChan <- mul(i) | |
}(i) | |
} | |
}() | |
return intsChan | |
} | |
func mul(i int) int { | |
return i * i | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment