Golang:使用循环片段/地图的范围注册多个路线 [英] Golang: Register multiple routes using range for loop slices/map

查看:176
本文介绍了Golang:使用循环片段/地图的范围注册多个路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  paths:= [] string {/ path0,/ path1 ,/ path2/ * .../ path-n* /} 
//其中n是最后一个路径

使用包 net / http ,我想使用使用范围子句循环。这是我如何做到这一点︰

$ p $ 为_,路径:=范围路径{
http.HandleFunc(路径,处理程序)
}
//在这种情况下,每个处理程序都将打印到控制台或浏览器的路径

但是我得到了相同的输出,它是slice的最后一个元素,所以当我到 / path1 时,输出是 /路径-N 。总是打印 / path-n



但是如果我使用这个:

  http.HandleFunc(paths [0],handler)
http.HandleFunc(paths [1],handler)
http.HandleFunc(paths [2],handler)
// ...
http.HandleFunc(paths [n],handler)

输出是正确的。



发生了什么,我错过了什么?我需要作为循环来注册路径或地图,所以我不能做第二个代码。



<你可以给我另一种方法来完成这个任务吗?解决方案

/ p>

  for _,path:= range paths {
http.HandleFunc(path,func(w http.ResponseWriter,req * http.Request){
fmt.Fprintf(w,path)
))
}

你使用了一个函数literal,一个闭包作为处理函数来注册。关闭捕获所涉及的上下文,在您的情况下为路径循环变量。



但是只有一个路径循环变量,其值在循环的每次迭代中被覆盖,其最终值将是最后一个路径。规范中的相关部分:对于范围子句的语句
$ b


迭代变量可以使用短变量声明:= )。在这种情况下,他们的类型被设置为各自的迭代值的类型,他们的范围是for语句块; 每次迭代都会重复使用。如果迭代变量是在for语句之外声明的,那么在执行之后它们的值将是最后一次迭代的值。


for循环已完成,并且您开始发出请求,每个已注册的处理程序函数将发回这个路径的值变量。这就是为什么你会看到为所有请求的路径返回的最后一个路径。

解决方法很简单:在每个迭代中创建一个新变量,并在处理函数中使用它:

  for _,path:= range paths {
path2:= path
http.HandleFunc(path2,func (w http.ResponseWriter,req * http.Request){
fmt.Fprintf(w,path2)
})
}

这里发生的是我们使用 short变量声明来创建一个 new 变量,并用路径循环变量的值初始化。我们注册的处理程序函数将引用这个新的变量,唯一的唯一注册路径。



另一个同样好的解决方案是使用一个带参数的匿名函数传递路径字符串。可能很难理解:

$ p $ for _,path:= range paths {
func(p string){
http.HandleFunc(p,func(w http.ResponseWriter,req * http.Request){
fmt.Fprintf(w,p)
})
}





$ b

这里发生的是我们调用一个匿名函数,传递当前的 path 值,它只使用这个匿名函数的参数(并且为每个调用分配一个新的,不同的局部变量)来注册处理函数。


Consider I have slice of string paths:

paths := []string{"/path0", "/path1", "/path2" /*... "/path-n"*/ }
// where n is the last path

Using package net/http, I want to register handler for this path using for loop with range clause. This is how I do this:

for _, path := range paths {
    http.HandleFunc(path, handler)
}
// in this case every handler is print the path to the console or to the browser

But I ended up with same output which is the last element of slice, so when I go to /path1, the output is /path-n. Same behavior with other element, always print /path-n.

But if I use this:

http.HandleFunc(paths[0], handler)
http.HandleFunc(paths[1], handler)
http.HandleFunc(paths[2], handler)
// ...
http.HandleFunc(paths[n], handler)

The output is correct.

What's going on, did I miss something? I need for loop for registration given by slice of paths or map, so I can't do the second code.

Can you give me the alternative to accomplished this task?

解决方案

So the problem was that you actually used this code:

for _, path := range paths {
    http.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, path)
    })
}

You used a function literal, a closure as the handler function to register. Closures capture the context they refer to, in your case the path loop variable.

But there is only a single path loop variable, its value is overwritten in each iterations of the loop, and its final value will be the last path. Relevant section from the spec: For statements with range clause:

The iteration variables may be declared by the "range" clause using a form of short variable declaration (:=). In this case their types are set to the types of the respective iteration values and their scope is the block of the "for" statement; they are re-used in each iteration. If the iteration variables are declared outside the "for" statement, after execution their values will be those of the last iteration.

Once the for loop is finished, and you start making requests, each registered handler function will send back the value of this single path variable. That's why you see the last path returned for all requested paths.

Solution is easy: create a new variable in each iteration, and use that in the handler function:

for _, path := range paths {
    path2 := path
    http.HandleFunc(path2, func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, path2)
    })
}

What happens here is that we use a short variable declaration in each iteration to create a new variable, initialized with the value of the path loop variable. And the handler function we register will refer to this new variable, unique only to one registered path.

Another, equally good solution is to use an anonymous function with a parameter to pass the path string. Might be harder to understand though:

for _, path := range paths {
    func(p string) {
        http.HandleFunc(p, func(w http.ResponseWriter, req *http.Request) {
            fmt.Fprintf(w, p)
        })
    }(path)
}

What happens here is that we call an anonymous function, passing the current path value to it, and it registers the handler function, using only the parameter of this anonymous function (and there's a new, distinct local variable allocated for each call).

这篇关于Golang:使用循环片段/地图的范围注册多个路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆