Last active
February 1, 2018 09:22
-
-
Save user454322/e265a977bae345ec5ee71de39593a8f0 to your computer and use it in GitHub Desktop.
Go channel
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" | |
"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