Why in this case a got an error all goroutins are asleep

package main

import (
  "fmt"
)

func main() {
type EXP struct {
	A string
	b string
}

ch := make(chan EXP)
aw := EXP{"asdasd", "ASDASD"}
ch <- aw

for i := range ch {
	fmt.Println(i)
  }
}

Because ch is unbuffered, your main-goroutine will therefore sleep on ch <- aw until another go routine reads from the channel.

1 Like

You can’t perform both send and receive operations of a channel if the channel is unbuffered, within the same goroutine.

You can hardly read and write to the same channel from one gouroutine. The example above would reach deadlock even if the channel was buffered. Because range over a channel will lock then all items in channel is read.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.