Created
April 8, 2020 06:51
-
-
Save doron2402/411f184f2755303ce8ccc10d762d7dd6 to your computer and use it in GitHub Desktop.
Go Channel - Example
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
// 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