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.
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 }
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
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.
« 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.
« 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.