Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http server support? #24

Closed
btfak opened this issue May 12, 2015 · 2 comments
Closed

http server support? #24

btfak opened this issue May 12, 2015 · 2 comments

Comments

@btfak
Copy link

btfak commented May 12, 2015

Hello,

I'm trying debug code like this:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    _ = "breakpoint"
    var count int
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, world") })
    go log.Fatal(http.ListenAndServe(":8080", nil))
    fmt.Println(count)
}

but it stop at line

go log.Fatal(http.ListenAndServe(":8080", nil))

image

any one help me?

@nussjustin
Copy link

The bug is in your code. Calling go log.Fatal(http.ListenAndServe(":8080", nil)) is the same as:

err := http.ListenAndServe(":8080", nil) // blocks the goroutine
go log.Fatal(err)

You are only calling log.Fatal in another goroutine. The evaluation of the arguments (in this case your http.ListenAndServe call) still happens in the calling goroutine, which in your case is the main goroutine. This blocks your main goroutine including the debugger.

You can fix your code with an anonymous function:

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

@btfak
Copy link
Author

btfak commented May 12, 2015

thanks,solved
@nuss-justin

@btfak btfak closed this as completed May 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants