The site at localhost:1234 can't be reached.

84 views
Skip to first unread message

loc...@gmail.com

unread,
Sep 17, 2016, 3:50:03 PM9/17/16
to golang-nuts
Hello everyone,

I am using Go version 1.7.1 on a 32-bit version of Windows 10 and cannot view the contents of the address localhost:1234. 
I created a server at the aforementioned address or at least, I think I did and get nothing but an error message when I try to open my browser.

Here's my code:

package main
import (
"io"
"net/http"
"log"
)
func main(){
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "Golang web development")
log.Fatal(http.ListenAndServe("localhost:1234", nil))
})
}

Dave Cheney

unread,
Sep 17, 2016, 3:58:20 PM9/17/16
to golang-nuts, loc...@gmail.com
Move http.ListenAndServe to the top of the main() function, outside the http.HandleFunc call and it will work.

Shawn Milochik

unread,
Sep 17, 2016, 3:59:18 PM9/17/16
to golang-nuts
The ListenAndServe shouldn't be in the handler function.

When you run this app, does it exit immediately? Because it looks like it exits immediately. Not serving anything.

Marvin Renich

unread,
Sep 17, 2016, 8:20:56 PM9/17/16
to golang-nuts
* Dave Cheney <da...@cheney.net> [160917 15:58]:
> Move http.ListenAndServe to the top of the main() function, outside the
> http.HandleFunc call and it will work.

Don't you mean bottom? At the top, the ListenAndServe will not return,
so the HandleFunc call will never be made, so you will always get 404.

...Marvin

Dave Cheney

unread,
Sep 17, 2016, 8:24:59 PM9/17/16
to golang-nuts
Yes. The bottom.

loc...@gmail.com

unread,
Sep 18, 2016, 3:55:56 AM9/18/16
to golang-nuts, loc...@gmail.com
Thank you everyone.
Reply all
Reply to author
Forward
0 new messages