-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathchannels.go
32 lines (25 loc) · 940 Bytes
/
channels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import "fmt"
/*
Channels are the pipes that connect concurrent goroutines.
You can send values into channels from one goroutine and receive those values into another goroutine.
*/
// Channels illustrate channels in go
func Channels() {
// Create a new channel with make(chan val-type).
// Channels are typed by the values they convey (transmettre).
messages := make(chan string)
// Send a value into a channel using the channel <- syntax. Here we send "ping" to the messages channel we made above, from a new goroutine.
go func() {
messages <- "ping"
}()
// The <-channel syntax receives a value from the channel.
// Here we’ll receive the "ping" message we sent above and print it out.
msg := <-messages
fmt.Println(msg)
// go func() {
// m := <-messages
// fmt.Println(m)
// }()
// When we run the program the "ping" message is successfully passed from one goroutine to another via our channel.
}