遍历 Golang 结构体的键和值时,由于某种原因结果不一致。

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

Iterating through key and values of golang struct not giving same result for some reason

问题

我有一个类似这样的 Golang map:

var routes map[string]*rest.Route

我已经向其中添加了一堆键值对,如下所示:

routes["Index"] = &rest.Route{"GET", "/", handlers.GetIndex}
routes["ListStudents"] = &rest.Route{"GET", "/students", handlers.ListStudents}
routes["ViewStudent"] = &rest.Route{"GET", "/students/:id", handlers.ViewStudent}
routes["CreateStudent"] = &rest.Route{"POST", "/students", handlers.CreateStudent}
routes["UpdateStudent"] = &rest.Route{"PUT", "/students/:id", handlers.UpdateStudent}
routes["DeleteStudent"] = &rest.Route{"DELETE", "/students/:id", handlers.DeleteStudent}

当我调用一个名为 SetRoute 的函数,并传入单个值时,它可以正常工作,如下所示:

handler.SetRoutes(routes["Index"])

然而,当我尝试使用 range 遍历该 map 时,它不起作用。我知道它不起作用是因为之后进行了一些测试。然而,如果我在调用 SetRoute 时打印出键和值,我可以看到键和值是我期望的。具体来说,这些值是地址。

// 这段代码不起作用,尽管打印出来的 k、v 没有问题...
for k, v := range routes {
    err := handler.SetRoutes(v)
    fmt.Println(k)
    fmt.Println(v)
    if err != nil {
        log.Fatal(err)
    }
}

打印出来的结果是:

Index
&{GET / 0x442250}
ListStudents
&{GET /students 0x4422f0}
ViewStudent
&{GET /students/:id 0x4427c0}
UpdateStudent
&{PUT /students/:id 0x443520}
DeleteStudent
&{DELETE /students/:id 0x443a30}
CreateSchool
&{POST /schools 0x444660}
CreateStudent
&{POST /students 0x442ed0}
ListSchools
&{GET /schools 0x443d50}
ViewSchool
&{GET /schools/:id 0x444200}
UpdateSchool
&{PUT /schools/:id 0x444cc0}
DeleteSchool
&{DELETE /schools/:id 0x4453b0}

我做错了什么?

英文:

I have a golang map like this:

var routes map[string]*rest.Route

I've added a bunch of key/values to it like so:

routes["Index"] = &rest.Route{"GET", "/", handlers.GetIndex}
routes["ListStudents"] = &rest.Route{"GET", "/students", handlers.ListStudents}
routes["ViewStudent"] = &rest.Route{"GET", "/students/:id", handlers.ViewStudent}
routes["CreateStudent"] = &rest.Route{"POST", "/students", handlers.CreateStudent}
routes["UpdateStudent"] = &rest.Route{"PUT", "/students/:id", handlers.UpdateStudent}
routes["DeleteStudent"] = &rest.Route{"DELETE", "/students/:id", handlers.DeleteStudent}

When I call a function, SetRoute on individual values, it works, like so:

handler.SetRoutes(routes["Index"])

However, when I try to iterate through the map with range, it doesn't work. I know it doesn't work because of testing that happens afterwards. However, if I print out the keys and values as I call SetRoute, I can see that the keys and values are what I expect. Specifically, the values are addresses.

// This doesn't work, even though printing out k,v shows nothing wrong...
for k, v := range routes {
	err := handler.SetRoutes(v)
	fmt.Println(k)
	fmt.Println(v)
	if err != nil {
		log.Fatal(err)
	}
}

Printing it out shows:

Index
&{GET / 0x442250}
ListStudents
&{GET /students 0x4422f0}
ViewStudent
&{GET /students/:id 0x4427c0}
UpdateStudent
&{PUT /students/:id 0x443520}
DeleteStudent
&{DELETE /students/:id 0x443a30}
CreateSchool
&{POST /schools 0x444660}
CreateStudent
&{POST /students 0x442ed0}
ListSchools
&{GET /schools 0x443d50}
ViewSchool
&{GET /schools/:id 0x444200}
UpdateSchool
&{PUT /schools/:id 0x444cc0}
DeleteSchool
&{DELETE /schools/:id 0x4453b0}

What am I doing wrong?

答案1

得分: 1

似乎handler.SetRoutes是可变参数的。你可以将多个路由传递给它。

以下代码应该可以工作:

routes := []rest.Route{
  rest.Route{"GET", "/", handlers.GetIndex},
  rest.Route{"GET", "/students", handlers.ListStudents},
  // 更多路由
}
handler.SetRoutes(routes...)
英文:

It seems that handler.SetRoutes is variadic. You can pass multiple routes to it.

This should work:

routes := []rest.Route{
  rest.Route{"GET", "/", handlers.GetIndex},
  rest.Route{"GET", "/students", handlers.ListStudents},
  // more routes
}
handler.SetRoutes(routes...)

答案2

得分: 0

原文翻译如下:

原来这不是一个golang的问题,而是一个go-json-rest的问题,这是我在Go中使用的库来创建RESTful API。

实际上,SetRoutes函数并不是添加每个后续的值,而是简单地替换。所以无论我调用多少次,只有最后一次调用的值会被保留。

英文:

So it turns out this isn't a golang issue, but a go-json-rest issue, which is the library I'm using to create a RESTful API in Go.

Essentially, the SetRoutes function isn't ADDING each subsequent value, but simply replacing. So no matter how many times I call it, only the last call will actually be persisted.

答案3

得分: 0

SetRoutes应该只被调用一次。此接口已被弃用。你应该使用v3 API,我认为它更不容易混淆:

api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
    // 在这里添加你的路由...
)
if err != nil {
    log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
英文:

SetRoutes was supposed to be called only once. Also this interface is deprecated. you should use the v3 API, which I think is less confusing:

    api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
    // your routes here ...
)
if err != nil {
    log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))

huangapple
  • 本文由 发表于 2014年5月9日 03:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/23550851.html
匿名

发表评论

匿名网友

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

确定