Golang:使用循环片段/地图的范围注册多个路线 [英] Golang: Register multiple routes using range for loop slices/map
问题描述
paths:= [] string {/ path0,/ path1 ,/ path2/ * .../ path-n* /}
//其中n是最后一个路径
使用包 但是我得到了相同的输出,它是slice的最后一个元素,所以当我到 但是如果我使用这个: 输出是正确的。 发生了什么,我错过了什么?我需要 net / http
,我想使用为$ c注册此路径的处理程序$ c>使用范围子句循环。这是我如何做到这一点︰
$ p $ 为_,路径:=范围路径{
http.HandleFunc(路径,处理程序)
}
//在这种情况下,每个处理程序都将打印到控制台或浏览器的路径
/ 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语句之外声明的,那么在执行之后它们的值将是最后一次迭代的值。
解决方法很简单:在每个迭代中创建一个新变量,并在处理函数中使用它: 这里发生的是我们使用 short变量声明来创建一个 new 变量,并用 另一个同样好的解决方案是使用一个带参数的匿名函数传递 这里发生的是我们调用一个匿名函数,传递当前的 Consider I have slice of string paths: Using package But I ended up with same output which is the last element of slice, so when I go to But if I use this: The output is correct. What's going on, did I miss something? I need Can you give me the alternative to accomplished this task? So the problem was that you actually used this code: You used a function literal, a closure as the handler function to register. Closures capture the context they refer to, in your case the But there is only a single The iteration variables may be declared by the "range" clause using a form of short variable declaration ( Once the Solution is easy: create a new variable in each iteration, and use that in the handler function: 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 Another, equally good solution is to use an anonymous function with a parameter to pass the What happens here is that we call an anonymous function, passing the current 这篇关于Golang:使用循环片段/地图的范围注册多个路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! for循环已完成,并且您开始发出请求,每个已注册的处理程序函数将发回这个
路径的值
变量。这就是为什么你会看到为所有请求的路径返回的最后一个路径。
for _,path:= range paths {
path2:= path
http.HandleFunc(path2,func (w http.ResponseWriter,req * http.Request){
fmt.Fprintf(w,path2)
})
}
路径
循环变量的值初始化。我们注册的处理程序函数将引用这个新的变量,唯一的唯一注册路径。
路径
字符串。可能很难理解:
$ p $ for _,path:= range paths {
func(p string){
http.HandleFunc(p,func(w http.ResponseWriter,req * http.Request){
fmt.Fprintf(w,p)
})
}
$ b path
值,它只使用这个匿名函数的参数(并且为每个调用分配一个新的,不同的局部变量)来注册处理函数。paths := []string{"/path0", "/path1", "/path2" /*... "/path-n"*/ }
// where n is the last path
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
/path1
, the output is /path-n
. Same behavior with other element, always print /path-n
.http.HandleFunc(paths[0], handler)
http.HandleFunc(paths[1], handler)
http.HandleFunc(paths[2], handler)
// ...
http.HandleFunc(paths[n], handler)
for
loop for registration given by slice of paths or map, so I can't do the second code.for _, path := range paths {
http.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, path)
})
}
path
loop variable.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:
:=
). 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.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.for _, path := range paths {
path2 := path
http.HandleFunc(path2, func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, path2)
})
}
path
loop variable. And the handler function we register will refer to this new variable, unique only to one registered path.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)
}
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).