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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。