Skip to content

Instantly share code, notes, and snippets.

@doron2402
Created April 8, 2020 06:51
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 doron2402/411f184f2755303ce8ccc10d762d7dd6 to your computer and use it in GitHub Desktop.
Save doron2402/411f184f2755303ce8ccc10d762d7dd6 to your computer and use it in GitHub Desktop.
Go Channel - Example
// Go channel example
package main
import (
"fmt"
"sync"
)
// send only function
func sendOnlyMessage(ch chan<- int, wg *sync.WaitGroup) {
ch <- 42
wg.Done()
}
func recieveOnlyMessage(ch <-chan int, wg *sync.WaitGroup) {
fmt.Println(<-ch)
wg.Done()
}
func main() {
wg := &sync.WaitGroup{}
ch := make(chan int) // channel default to 0 messages
wg.Add(2)
go sendOnlyMessage(ch, wg)
go recieveOnlyMessage(ch, wg)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment