當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO HandleFunc用法及代碼示例

GO語言"net/http"包中"HandleFunc"函數的用法及代碼示例。

用法:

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

HandleFunc 在DefaultServeMux 中為給定模式注冊處理函數ServeMux 的文檔解釋了模式是如何匹配的。

例子:

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

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 HandleFunc。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。