Skip to content

HTTP server

Go’s net/http package makes it easy to build HTTP servers. Use http.HandleFunc and http.ListenAndServe.

A simple server that responds with “Hello, World”.

package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Terminal window
Hello, Alice!