Skip to content

Instantly share code, notes, and snippets.

@haisum
Created January 30, 2015 05:01
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 haisum/dbd8b1fbc593027baa3f to your computer and use it in GitHub Desktop.
Save haisum/dbd8b1fbc593027baa3f to your computer and use it in GitHub Desktop.
Simple Threads/Channels code in golang
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