I am trying to learn web programming with Go. I stared out with a simple "hello world" web server:
package main
import "fmt"
import "net/http"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
And when I go to
http://localhost:8080/handler
in the browser, the browser can´t seem to find anything and nothing happens. What could be the reason for this?
You mapped your handler to the root ("/"
) of your server.
Call it like this in your browser
http://localhost:8080/
If you want to map a service to a specific name you can do this :
http.HandleFunc("/something", handler)
Then you would type this in your browser :
http://localhost:8080/something