http.Server - 获取URL片段

iva*_*oid 2 go

使用标准提取片段数据(foo在http://domain.com/path#foo中)没有运气http.Server.

package main

import (
    "fmt"
    "net/http"
)

type Handler struct {
}

func (handler Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("Path = \"%v\"  Fragment = \"%v\"\n", r.URL.Path, r.URL.Fragment)
}

func main() {
    var handler Handler
    http.ListenAndServe(":30000", handler)
}
Run Code Online (Sandbox Code Playgroud)

产生空片段http://127.0.0.1:30000/path#foo:

Path = "/path" Fragment = ""

如何使用golang的内置程序获取片段数据http.Server?

Ism*_*awi 11

你不能.这不是Go事物 - URL片段不会通过HTTP发送到服务器.它们只是一个浏览器概念.

这是一个相关的问题,其中的文档http.Request被更改为:

// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI.  For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 2616, Section 5.1.2)
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因你需要它,你可能会将一些JavaScript串在一起,将请求中的片段作为GET参数或其他东西包含在内.