(And I really should hit "reply to all", lol)

> https://go.dev/ref/spec#Go_statements :
>
> > The function value and parameters are evaluated as usual 
<https://go.dev/ref/spec#Calls> *in the calling goroutine*, but unlike with 
a regular call, program execution does not wait for the invoked function to 
complete.
*(emphasis mine)*

This explains it. I really should read the spec a bit more often. :)

Thanks Sean & Robert!

On Friday, February 18, 2022 at 5:50:04 PM UTC+1 seank...@gmail.com wrote:

> https://go.dev/ref/spec#Go_statements :
>
> > The function value and parameters are evaluated as usual 
> <https://go.dev/ref/spec#Calls> in the calling goroutine, but unlike with 
> a regular call, program execution does not wait for the invoked function to 
> complete.
>
> http.ListenAndServe is being evaluated, but it blocks.
> Only the log.Println call would run in the newly started goroutine.
>
> On Friday, February 18, 2022 at 5:35:52 PM UTC+1 christoph...@gmail.com 
> wrote:
>
>> Hi gophers, 
>>
>> I hope someone can help finding out why a particular goroutine call can 
>> block the main goroutine.
>>
>> In the following code, all "go" calls spawn off a goroutine and return as 
>> expected, except for the last one that blocks the main goroutine. 
>>
>> Is this a bug, or am I missing something subtle or even obvious (obvious 
>> to all others only of course)?
>>
>> package main
>>
>> import (
>>         "log"
>>         "net/http"
>> )
>>
>> func main() {
>>         // all of these work as expected
>>         go http.ListenAndServe("localhost:8080", nil)
>>         go log.Println("goroutine")
>>         go func() {
>>                 log.Println(http.ListenAndServe("localhost:8081", nil))
>>         }()
>>
>>         // The following line blocks the main goroutine.
>>         go log.Println(http.ListenAndServe("localhost:8082", nil))
>>
>>         log.Println("after go log.Println(http.ListenAndServe())") // 
>> never prints
>>         select {} // remove this, and the code still never finishes
>> }
>>
>> All three servers eventually run (try curl localhost:8080; curl 
>> localhost:8081; curl localhost:8082),
>>
>> In the playground <https://go.dev/play/p/vObgGGM7MJD>, the code even 
>> deadlocks. 
>>
>> Any idea?
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c22c5a37-5df5-45ae-810c-7616f2ff34a2n%40googlegroups.com.

Reply via email to