Похожие чаты

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 ответов

22 просмотра

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.

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

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

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
16
any reference of this implementation?
BitBuddha
29
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Карта сайта