Golang http helloworld not blocking

My code is the same as in gowiki

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

      

However, after I build and run this program, it unloads immediately without blocking, so I get no response when I try to access http://localhost:8080/monkey

from Chrome.

Environment: Ubuntu 14 (in VirtualBox on Windows7)

Why?

+3


source to share


2 answers


Check the error returned from ListenAndServe



func main() {
    http.HandleFunc("/", handler)
    fmt.Println(http.ListenAndServe(":8080", nil))
}

      

+13


source


Function

http.ListenAndServe

returns an object corresponding to the interface error

. If the call does not block, it definitely means that some kind of error has occurred. Most Popular:



  • there is already another process listening on the port
  • your user is not allowed to bind a socket on a port 8080

    or 0.0.0.0

    interface
+3


source







All Articles