Go-net/http-func HandleFunc

来自泡泡学习笔记
BrainBs讨论 | 贡献2024年2月2日 (五) 09:09的版本 (创建页面,内容为“ HandleFunc 在 DefaultServeMux 中为给定模式注册处理函数。 <syntaxhighlight lang="go">func HandleFunc(pattern string, handler func(ResponseWriter, *Request))</syntaxhighlight> <br> <syntaxhighlight lang="go">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.…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

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))
}