Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang http helloworld doesn't block

Tags:

http

go

My code is just 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 exit immediately without blocking so that I get no response when I try to access http://localhost:8080/monkey from Chrome.

Environment: Ubuntu 14(in VirtualBox on Windows7)

Why?

like image 424
dastan Avatar asked Nov 28 '22 01:11

dastan


1 Answers

Check the error returned from ListenAndServe

func main() {
    http.HandleFunc("/", handler)
    fmt.Println(http.ListenAndServe(":8080", nil))
}
like image 189
OneOfOne Avatar answered Dec 16 '22 03:12

OneOfOne