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


GO StripPrefix用法及代码示例

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

用法:

func StripPrefix(prefix string, h Handler) Handler

StripPrefix 返回一个处理 HTTP 请求的处理程序,方法是从请求 URL 的路径(和 RawPath,如果设置)中删除给定的前缀并调用处理程序 h。 StripPrefix 通过回复 HTTP 404 not found 错误来处理对不以前缀开头的路径的请求。前缀必须完全匹配:如果请求中的前缀包含转义字符,则回复也是 HTTP 404 not found 错误。

例子:

package main

import (
    "net/http"
)

func main() {
    // To serve a directory on disk (/tmp) under an alternate URL
    // path (/tmpfiles/), use StripPrefix to modify the request
    // URL's path before the FileServer sees it:
    http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
}

相关用法


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