Go-net/http-func HandleFunc
跳到导航
跳到搜索
HandleFunc 在 DefaultServeMux 中为给定模式注册处理函数。
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
package main
import (
"io"
"log"
"net/http"
)
func main() {
h1 := func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello from a HandleFunc #1!\n")
}
h2 := func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello from a HandleFunc #2!\n")
}
http.HandleFunc("/", h1)
http.HandleFunc("/endpoint", h2)
log.Fatal(http.ListenAndServe(":8080", nil))
}