Your receive()
method doesn't use the lock, so send()
holding the lock has no effect on receive()
.
And since both send()
and receive()
run in their own goroutine, send()
will make it to the point where it sends the value 5
on the channel, and so the receive in receive()
can proceed, and it will, and will print it in the next line.
Also note that to use channels from multiple goroutines, you do not need "external" synchronization. Channels are safe for concurrent use, data races cannot occur by design. For details, see If I am using channels properly should I need to use mutexes?
If the receive()
method would also use the lock like this:
func (s *S) receive() {
s.mu.Lock()
num := <-s.ch
s.mu.Unlock()
fmt.Printf("%d\n", num)
}
Then yes, nothing would be printed, because the receive cannot happen until send()
releases the lock, but that can't happen until someone receives from the channel.
And in this case the program would terminate after 1 second without printing anything, because when the sleep is over the main goroutine ends, so does your whole app with it. It does not wait for other non-main goroutines to complete. For details, see No output from goroutine in Go.
Edit:
Yes, you misunderstand the lock. Locking a sync.Mutex
only locks the mutex value itself, it does not lock the whole struct value (it can't). And "locks the value itself" means if another goroutine also calls its Mutex.Lock()
method, that call will block until the lock is released by calling its Mutex.Unlock()
method. When it's unlocked, the goroutine that is blocked at the Mutex.Lock()
call will proceed to lock the mutex and return.