Missing last element from channel

Hello everyone

I have this code as example. Something similar happens to me in a bigger project I am working on.


Can anyone explain to me why the last number is lost? When I execute the code 9 is missing. I do not understand why it happens.


Looks like I was doing all wrong :blush:

I noticed my error, here the solution:


package main

import (

func main() {
    numbers := make(chan int)
    go func() {
        for i := 0; i < 100; i++ {
            numbers <- i

    for n := range numbers {

When main() ends, the whole program stops, even if a running goroutine hasn’t completed.
Add wait logic like this:


All your concurrency questions answer is this absolutely FABULOUS book:

Concurrency in Go
by Katherine Cox-Buday
Released August 2017
Publisher(s): O’Reilly Media, Inc.
ISBN: 9781491941294

Don’t hesitate. Just buy the book, and read it. You will be glad you did.

Good luck.

Jeff Kayser

1 Like