Vol. 2 — № 01
The Go Loop · KiosqueNewsstand
Blog  
Un atelier Go · Édition d'ApprentissageA Go Workshop · Learning Edition

Le channel, une valeur qui change de main The channel, a value changing hands

Un channel n'est pas une file d'attente : c'est un point de rendez-vous où la propriété d'une donnée passe d'une goroutine à l'autre. Bufferisé ou non, c'est la même idée — envoyer plutôt que partager derrière un verrou. Le cœur du slogan : partage la mémoire en communiquant. A channel isn't a queue: it's a rendezvous where a value's ownership passes from one goroutine to another. Buffered or not, same idea — send rather than share behind a lock. The heart of the slogan: share memory by communicating.

AudienceAudience
Dev qui a fait passer des données dans un channel sans se demander à qui elles appartenaient Dev who has pushed data through a channel without asking who owned it
Format
Self-paced
ChapitresChapters
5
Date
Sep 2026 Sep 2026
≈ 18 min ●●○○ ChannelsCommunication

Chapitre 1 en accès libre — la suite (ch. 2 à 5) est réservée. Chapter 1 free to read — the rest (ch. 2–5) is members-only.

01CadrageFraming3 min

Un channel n'est pas une file d'attente. C'est un passage de main.A channel isn't a queue. It's a handoff.

Le volume 1 lançait des goroutines et les rejoignait ; la donnée, elle, ne faisait que transiter sans qu'on l'interroge. On ouvre le volume 2 sur ce transit lui-même. La plupart des langages synchronisent en protégeant une mémoire partagée derrière un verrou. Go propose l'inverse : au lieu de garder la valeur et d'en réguler l'accès, on la transmet — et celui qui la reçoit en devient seul propriétaire. C'est tout le slogan : ne communique pas en partageant la mémoire, partage la mémoire en communiquant.Volume 1 launched goroutines and rejoined them; the data merely passed through, unquestioned. Volume 2 opens on that passage itself. Most languages synchronize by guarding shared memory behind a lock. Go proposes the opposite: instead of keeping the value and regulating access to it, you hand it over — and whoever receives it becomes its sole owner. That's the whole slogan: don't communicate by sharing memory; share memory by communicating.

Partager la mémoire — et la défendreSharing memory — and defending it
var mu sync.Mutex
var total int

func add(n int) {
    mu.Lock()              // tout le monde touche la MÊME case
    total += n             // … à condition de bien prendre le verrou
    mu.Unlock()            // un oubli, et c'est la data race
}
Communiquer la valeur — et la lâcherCommunicating the value — and letting go
var results = make(chan int)

func worker(n int) {
    results <- n*n         // je calcule chez moi, puis je TRANSMETS
}                          // personne d'autre ne touche ma valeur
Le témoin change de mainThe baton changes hands
goroutine Agoroutine A
propriétaireowner
ch <- v
goroutine Bgoroutine B
nouveau propriétairenew owner

Un envoi n'est pas une copie laissée dans une boîte : c'est un témoin de relais. À l'instant où B le reçoit, A n'a plus à y toucher — et n'y touche plus. La valeur n'est jamais à deux mains en même temps : voilà pourquoi il n'y a ni verrou, ni course.A send isn't a copy left in a box: it's a relay baton. The instant B receives it, A no longer has to touch it — and doesn't. The value is never in two hands at once: that's why there's no lock, and no race.

Le réflexe du numéroThe issue's reflex

« Transmets la valeur, puis lâche-la. » Tout ce numéro tient dans cette discipline : ce qu'on envoie, on cesse d'y toucher. Le channel garantit le moment du passage ; c'est à toi de garantir qu'aucune main ne reste accrochée derrière."Hand the value over, then let go." This whole issue lives in that discipline: what you send, you stop touching. The channel guarantees the moment of the handoff; it's on you to guarantee no hand stays clutching behind.

Le slogan, et sa petite trahison.The slogan, and its small betrayal.

« Don't communicate by sharing memory; share memory by communicating » est le proverbe fondateur de la concurrence en Go. Mais il décrit une intention, pas une garantie : rien dans le compilateur ne t'empêche d'envoyer un pointeur et de continuer à écrire derrière. Le channel déplace la propriété par convention ; tenir cette convention reste ton travail. Tout le numéro mène à ce piège — chapitre 4.'Don't communicate by sharing memory; share memory by communicating' is Go's founding proverb of concurrency. But it states an intent, not a guarantee: nothing in the compiler stops you from sending a pointer and going on writing behind it. The channel moves ownership by convention; honoring that convention stays your job. The whole issue builds to that trap — chapter 4.

🔒

La suite est réservée The rest is members-only

Le premier numéro est libre. Débloque tout The Go Loop — tous les volumes, à vie — pour 5 €, paiement unique. The first issue is free. Unlock all of The Go Loop — every volume, forever — for €5, one-time.

Retour au kiosqueBack to newsstand