When trying to ListenAndServer
inside a go routine I get an error:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
http.HandleFunc("/static/", myHandler)
go func() {
http.ListenAndServe("localhost:80", nil)
}()
fmt.Printf("we are here")
resp, _ := http.Get("localhost:80/static")
ans, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("response: %s", ans)
}
func myHandler(rw http.ResponseWriter, req *http.Request) {
fmt.Printf(req.URL.Path)
}
The error:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x48 pc=0x401102]
goroutine 1 [running]:
panic(0x6160c0, 0xc0420080a0)
c:/go/src/runtime/panic.go:500 +0x1af
main.main()
C:/gowork/src/exc/14.go:20 +0xc2
exit status 2
All I want is to create an http
server. And then test it and connect to it from the code. What's wrong with Go? (or with me?)