在Go语言中,当谈到HTTP请求时,”CONTEXT”是什么意思?

huangapple go评论80阅读模式
英文:

What is this CONTEXT in Go lang when talking about HTTP Requests?

问题

对于一些人来说,这可能是一个愚蠢的问题。然而对我来说,这非常重要。我刚开始学习Go编程,当我阅读这篇在中间件之间共享值和这篇Go并发模式:上下文时,我无法弄清楚在谈到在中间件处理程序或请求处理程序之间共享值时,它们所指的上下文到底是什么意思。

我已经用Go编写了一个Web框架——Frodo。虽然在很多方面还不完善,但它是一个学习实验。正如你所见,我并不愚蠢,只是对Go语言还不够了解。

非常感谢您提供一个全面的解释。

英文:

To some this might be a stupid question. On the other hand this is is very important to me. I am new to Go programming and when I read this Sharing values between Middlewares and this Go Concurrency Patterns: Context I could not figure out what exactly what they meant by Context when referring to sharing values between middleware handlers or request handlers in general.

I've been able to write a web framework with Go -- Frodo. Not perfect in a lot of ways, it was a learning experiment. As you see I am not stupid. Just under informed being new to Go lang.

Thank you in advance for a comprehensive explanation.

答案1

得分: 7

我在这篇博客的开头找到了一个很好的解释:HTTP请求上下文和Go

对于对这个术语不熟悉的人来说,请求上下文通常是一种在处理HTTP请求时传递数据的方式,这些请求由你编写的处理程序(或中间件)处理。这些数据可以是用户ID、CSRF令牌、Web令牌、用户是否已登录等等,通常是从你不想在每个处理程序中重复的逻辑中派生出来的。如果你曾经使用过Django,请求上下文就相当于request.META字典。

英文:

I found a great explanation at the beginning of this blog: HTTP Request Contexts & Go

> Request contexts, for those new to the terminology, are typically a
> way to pass data alongside a HTTP request as it is processed by
> handlers (or middleware) you have written.
> This data could be a user ID, a CSRF token, a web token,
> whether a user is logged in or not—something typically
> derived from logic that you don't want to repeat over-and-over again
> in every handler. If you've ever used Django, the request context is
> synonymous with the request.META dictionary.

huangapple
  • 本文由 发表于 2016年1月7日 23:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/34658423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定