Skip to content

Instantly share code, notes, and snippets.

@user454322
Last active February 1, 2018 09:22
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 user454322/e265a977bae345ec5ee71de39593a8f0 to your computer and use it in GitHub Desktop.
Save user454322/e265a977bae345ec5ee71de39593a8f0 to your computer and use it in GitHub Desktop.
Go channel
package main
import (
"fmt"
"time"
)
func multiply(arg float32, c chan float32) {
var val = arg * arg
for i := 0; i < 10; i++ {
fmt.Print(arg)
time.Sleep(100 * time.Millisecond)
}
fmt.Println("Done with ", arg)
c <- val // send sum to c
}
func main() {
channel := make(chan float32)
go multiply(1.0, channel)
go multiply(2.0, channel)
x, y := <-channel, <-channel // receive from channel
fmt.Println("\n-- D O N E --")
fmt.Println("x", x)
fmt.Println("y", y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment