Official doc for http server

This commit is contained in:
Muyao CHEN 2024-09-05 16:23:58 +02:00
parent d97a2f3bca
commit b96656a469
2 changed files with 28 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.vinchent.xyz/vinchent/go-web
go 1.22.5

25
main.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"fmt"
"html"
"log"
"net/http"
)
type FooHandler struct{}
func (foo FooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
func main() {
var fooHandler FooHandler
http.Handle("/foo", fooHandler)
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}