Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在golang中获取http请求的初始时间戳?_Http_Go_Httprequest_Httpcontext_Revel - Fatal编程技术网

如何在golang中获取http请求的初始时间戳?

如何在golang中获取http请求的初始时间戳?,http,go,httprequest,httpcontext,revel,Http,Go,Httprequest,Httpcontext,Revel,我正在使用revel框架编写一个golang程序,其中需要检查http请求的初始时间戳 我知道如何用C#做: 在Go中没有太多的实现方法。.Net framework中的HttpContext类。您还可以将时间戳存储在请求处理程序函数的第一行。最简单的方法是获取处理程序中的当前时间 type Handler struct { } func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { rs := time.N

我正在使用revel框架编写一个golang程序,其中需要检查http请求的初始时间戳

我知道如何用C#做:


在Go中没有太多的实现方法。

.Net framework中的HttpContext类。您还可以将时间戳存储在请求处理程序函数的第一行。

最简单的方法是获取处理程序中的当前时间

type Handler struct {
}

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  rs := time.Now().UTC()

  //TODO: Use the time.
}
如果您想测量处理程序之前的所有中间件所花费的时间,那么您可以更新Go上下文,并将中间件放在中间件链的开头

下面是该中间件的示例:

package timemiddleware

import (
    "context"
    "net/http"
    "time"
)

// New returns new middleware which tracks the time that a request started.
func New(next http.Handler) http.Handler {
    return handler{
        next: next,
    }
}

type key int

const id = key(1)

type handler struct {
    next http.Handler
}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    ctx := context.WithValue(r.Context(), id, time.Now().UTC())
    h.next.ServeHTTP(w, r.WithContext(ctx))
}

// GetTime returns time from the current request, where it has previously been added by the middleware.
func GetTime(r *http.Request) (t time.Time, ok bool) {
    v := r.Context().Value(id)
    t, ok = v.(time.Time)
    return
}
您可以按照以下示例使用此选项:

包干管

import (
    "fmt"
    "net/http"
    "time"

    "github.com/xxxxx/timemiddleware"
)

func main() {
    next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(time.Second * 5)
        w.Write([]byte("Hello"))
        if t, ok := timemiddleware.GetTime(r); ok {
            fmt.Println(t)
            fmt.Println(time.Now().UTC())
        }
    })

    h := timemiddleware.New(next)
    fmt.Println(http.ListenAndServe("0.0.0.0:8080", h))
}
import (
    "fmt"
    "net/http"
    "time"

    "github.com/xxxxx/timemiddleware"
)

func main() {
    next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(time.Second * 5)
        w.Write([]byte("Hello"))
        if t, ok := timemiddleware.GetTime(r); ok {
            fmt.Println(t)
            fmt.Println(time.Now().UTC())
        }
    })

    h := timemiddleware.New(next)
    fmt.Println(http.ListenAndServe("0.0.0.0:8080", h))
}