Skip to content

Instantly share code, notes, and snippets.

@kamoljan
Created January 29, 2014 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamoljan/8681485 to your computer and use it in GitHub Desktop.
Save kamoljan/8681485 to your computer and use it in GitHub Desktop.
How to use Go Channel
package main
import (
"fmt"
)
func print(i int) string {
if i%3 == 0 {
if i%5 == 0 {
return fmt.Sprintf("%d PipipiPopopo\n", i)
}
return fmt.Sprintf("%d Popopo\n", i)
} else if i%5 == 0 {
return fmt.Sprintf("%d Pipip\n", i)
}
return fmt.Sprintf("%d\n", i)
}
func loop(f int, t int, c chan string) {
x := ""
for i := f; i <= t; i++ {
x = x + print(i)
}
c <- x
}
func main() {
c := make(chan string)
go loop(1, 50, c)
go loop(51, 100, c)
fmt.Println(<-c, <-c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment