why goroutine block main func in this http server?

LeoTao :

I want to setup a http server with httprouter listening on two ports 8888 and 8080 just like the code below.

package main

import (
    "fmt"
    "github.com/julienschmidt/httprouter"
    "log"
    "net/http"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func main() {
    router := httprouter.New()
    router.GET("/", Index)

    fmt.Println("listen on 8080")
    // this is where blocked
    go log.Fatal(http.ListenAndServe(":8080", router))
    fmt.Println("listen on 8888")
    log.Fatal(http.ListenAndServe(":8888", router))
}

But it doesn't work properly,my server only listen on 8080.If I make some change:

go func() { log.Fatal(http.ListenAndServe(":8080", router)) }()

It works finely both on 8080 and 8888.So why? It's about closure or something else?

hobbs :

The function value and parameters are evaluated as usual in the calling goroutine

Go language spec, "Go statements".

You're creating a goroutine for the call to log.Fatal, but the arguments to log.Fatal are evaluated beforehand, in the main goroutine. And Fatal's argument is the return value of http.ListenAndServe. So the new goroutine doesn't start until after ListenAndServe returns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this goroutine block?

Why do I not get a deadlock when trying to read from a channel that never receives data in a goroutine but do in the main func

reviced channel error on main func but no error in if reviced progrenn in goroutine

why input.Text() is evaluated in the main goroutine

Why isn't this go HTTP server spawning a goroutine per request in Chrome 47?

Reverse proxy with Cockpit uses HTTP2 when its server block doesn't, but main server block does

Why does the goroutine not block for time.Sleep(duration)

Why is the main goroutine blocked in this case thus resulting in a deadlock?

Golang http server blocks when starts a goroutine of infinite-loop

Why does ForkJoinPool::invoke() block the main thread?

Why does this subscription block the main thread?

Package "main" and func "main"

Is main just a normal goroutine?

Is the main function runs as a goroutine?

Why is the output for the char pointer p different for the main and the func function?

Why the POP() function in condition can really remove the main Func?

Does this goroutine leak/block forever?

Will time.Sleep block goroutine?

Why does my program not shut down when I use goroutine in main?

Issue modifying map from goroutine func

Wait on main thread with sleep in goroutine

main exiting to soon, due to goroutine

Main thread never yields to goroutine

If join blocks the main thread, why it doesn't block in loop?

Why is that constantly animating UIActivityIndicator won't block main thread?

Why does not WiringPiISR block the main routine when it is fired?

Why does Handler.Post Block the main Thread

Why goroutine leaks

Why this goroutine leaks?