Похожие чаты

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

9 просмотров

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.

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

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

Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
32
Хотел бы спросить у знающих, правильную ли я выбрал книгу для начала изучения ассемблера Юрова В.И ? Или есть более лучшие книги для начала обучения?
Botsman
25
Добрый день, не подскажите, если в OC-V3 поменять страндартную директорию /storage/ на /storage2/ - не будет сильно много проблем ?
Max Dubovsky
32
Книга Юрова В.И пойдёт для обучения?
Botsman
24
Conversation at a festival with a non-crpto person (not a normie by any stretch, though): * person: tell me about crypto, me: ok, the original idea is p2p sound money for the...
molecular#123 🐓
25
$params = [ 'formid' => 'feedbackForm', 'formTpl' => '@CODE: <form class="form-validate" data-id="ajax_form"> <fieldset class="margin-bottom-md"> ...
Pathologic
1
I bought some PHA tokens on Kucoin, which is the best wallet to store and easy to use?
AMR
19
Can you believe it? A whole new gaming ecosystem powered by FunToken! 😲
Josh
13
'frakturBold' => ['𝖆', '𝖇', '𝖈', '𝖉', '𝖊', '𝖋', '𝖌', '𝖍', '𝖎', '𝖏', '𝖐', '𝖑', '𝖒', '𝖓', '𝖔', '𝖕', '𝖖', '𝖗', '𝖘', '𝖙', '𝖚', '𝖛', '𝖜', '𝖝', '𝖞', '𝖟', '𝕬', '𝕭', '𝕮', '𝕯'...
Roma
4
Подскажите пожалуйста, а я могу вот такую штуку использовать? rpc, только реализованное в реббите https://www.rabbitmq.com/tutorials/tutorial-six-php ( или https://habr.com/ru...
Artyom
11
Карта сайта