当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Handle用法及代码示例

GO语言"net/http"包中"Handle"函数的用法及代码示例。

用法:

func Handle(pattern string, handler Handler)

Handle 在DefaultServeMux 中为给定模式注册处理程序ServeMux 的文档解释了模式是如何匹配的。

例子:

package main

import (
    "fmt"
    "log"
    "net/http"
    "sync"
)

type countHandler struct {
    mu sync.Mutex // guards n
    n  int
}

func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    h.mu.Lock()
    defer h.mu.Unlock()
    h.n++
    fmt.Fprintf(w, "count is %d\n", h.n)
}

func main() {
    http.Handle("/count", new(countHandler))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Handle。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。