Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 488 Bytes

File metadata and controls

25 lines (19 loc) · 488 Bytes

Writing web servers

The main function begins with a call to http.HandleFunc, which tells the http package to handle all requests to the web root ("/") with handler.

package main

import (
    "fmt"
    "log"
    "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)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
  • See examples: serv[n]