educative.io

Educative

Why this code doesn't work? (defer + range)

package main

import "fmt"

func sendValues(myIntChannel chan int){

  for i:=0; i<5; i++ {

    myIntChannel <- i //sending value 

  }

}

func main() {

  myIntChannel := make(chan int)

  defer close(myIntChannel)

  go sendValues(myIntChannel)

  for i := range myIntChannel {

    fmt.Println(i) //receiving value

  }

}