Похожие чаты

I’ve been exploring channels and playing around with some basic

edge cases. I understand for a successful transaction to occur in a channel, A sender and a receiver must be available, for a given “message”.

Why does the following operation not result in a deadlock?


package main

func main() {
messages := make(chan string)

// A go routine that sends to a channel
go func() {
messages <- "I am sending a message."
}()

// Main routine has no receiver.
// No compilation or runtime error.
// Why doesn't it deadlock?
}

11 ответов

23 просмотра

because your main goroutine doesn't wait, returns and the program is terminated

so if I understand correctly deadlock will only occur if you try to access the channel without goroutines because the channel has no buffer.

jeffotoni
so if I understand correctly deadlock will only oc...

package main func main() { messages := make(chan string) // A go routine that sends to a channel //go func() { messages <- "I am sending a message." //}() }

jeffotoni
package main func main() { messages := make(cha...

if you make a buffered channel it will work messages := make(chan string, 1)

Justin-K Автор вопроса
jeffotoni
if you make a buffered channel it will work messag...

Yes, I did play with buffer sizes. Unless I misunderstood, I read somewhere that buffer channels have their time and place. To limit their use and rather keep the code “tightly coupled” for each successful channel transaction to occur (or something). Is that a valid point?

Justin K
Yes, I did play with buffer sizes. Unless I misund...

When using buffering there is no need to use goroutine for example. When we don't have a buffer on the channel, the use of goroutines is mandatory.

Justin-K Автор вопроса
jeffotoni
When using buffering there is no need to use gorou...

Thanks, Yes, I understand that bit but the point I wrote just earlier, is that a valid consideration at all? That to use buffer only if the solution really requires of the channel to hold a few messages?

Justin K
Thanks, Yes, I understand that bit but the point I...

Yes, adaptation by need and problems we know the amount to be shared.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта