“variable only valid within handler” 的意思是“仅在处理程序内有效的变量”。

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

What is the meaning of variable only valid within handler

问题

在go-fiber文档中,他们说:

> 作为一个经验法则,你只能在处理程序中使用上下文值,并且不能保留任何引用。

如果我像这样将上下文作为函数参数传递,这样做可以吗?

func GetList(c *fiber.Ctx) error {
   result, err := User.Search(c)
}

func Search(c *fiber.Ctx) (User, err) {
   id := c.Params("id")
}

这被认为是一种不好的做法吗?

我真的不理解这句话的意思:

> 一旦你从处理程序返回,你从上下文中获取的任何值都将在将来的请求中被重用,并且会在你的脚下发生变化。

那么如果我已经将响应发送给客户端,params的值会被重用吗?被谁重用?是我还是其他人的请求?

func GetList(c *fiber.Ctx) error {
   id := c.Params("id") // 911
   return c.SendString("Hello, World!")
}

所以如果id的值是911,那么其他人的请求也会导致id为911吗?

还有"will change below your feet"的意思是什么?

能否有人用更简单的方式解释给像我这样的初学者理解?谢谢...

英文:

In go-fiber docs they say:

> As a rule of thumb, you must only use context values within the
> handler, and you must not keep any references

is it OK if I passing around the context as a function argument like this:

func GetList(c *fiber.Ctx) error {
   result, err := User.Search(c)
}

func Search(c *fiber.Ctx) (User, err) {
   id := c.Params("id")
}

is that considered as a bad practice?

And I don't really understand this sentence:

> As soon as you return from the handler, any values you have obtained
> from the context will be re-used in future requests and will change
> below your feet.

So if I have sent the response to the client the value of params will be reused? reused by whom? by me or by other people's request?

func GetList(c *fiber.Ctx) error {
   id := c.Params("id") // 911
   return c.SendString("Hello, World!")
}

so if the value of id was 911 does that mean other people request will also result in 911?

and what's the meaning of

> will change below your feet

can anyone elaborate more easy for beginner like me to understand? thanks...

答案1

得分: 1

实际的上下文对象在框架调用处理程序后可以被重用,因此您不能依赖于处理程序返回后的状态。

如果我像这样将上下文作为函数参数传递,这样可以吗?

这是可以的,只要Search不会将上下文存储在其他地方。如果它只是使用上下文中的值进行搜索,然后返回结果,那就没问题。

所以如果我已经将响应发送给客户端,params的值会被重用吗?被谁重用?是我还是其他人的请求?

实际的上下文对象将在处理后续请求时由框架重用。

"will change below your feet"的意思是什么?

如果您不遵循上述建议,而是在从处理程序返回后保留对上下文的引用,那么该上下文中的值将意外更改,因为框架正在将该上下文用于新的请求。

英文:

The actual context object can be reused by the framework after it calls your handler, so you cannot rely on its state after you return from the handler.

> is it OK if I passing around the context as a function argument like this?

This is fine, as long as Search doesn't store the context elsewhere. If it just uses values from the context to conduct a search and then returns a result, that's fine.

> So if I have sent the response to the client the value of params will be reused? reused by whom? by me or by other people's request?

The actual context object will be reused by the framework, while handling a later request.

> and what's the meaning of "will change below your feet"?

If you don't follow the advice above, and instead keep references to the context after returning from your handler, the values in that context will change unexpectedly, since the framework is using that context for a new request.

huangapple
  • 本文由 发表于 2022年11月21日 10:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/74513765.html
匿名

发表评论

匿名网友

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

确定