Created
January 30, 2015 05:01
-
-
Save haisum/dbd8b1fbc593027baa3f to your computer and use it in GitHub Desktop.
Simple Threads/Channels code in golang
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() { | |
message := make(chan string, 1) | |
response := make(chan string, 1) | |
done := make(chan bool, 1) | |
go func() { | |
fmt.Println("Routine One started") | |
for i := 0; i < 3; i++ { | |
time.Sleep(time.Second * 1) | |
message <- "sarmad" | |
fmt.Printf("\nMessage confirmation: %s\n", <-response) | |
} | |
done <- true | |
}() | |
go func() { | |
fmt.Println("Routine two started") | |
for { | |
fmt.Printf("\nMessage recieved: %s\n", <-message) | |
response <- "Received" | |
} | |
}() | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment