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

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

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

问题

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

  1. var routes map[string]*rest.Route

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

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

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

  1. handler.SetRoutes(routes["Index"])

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

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

打印出来的结果是:

  1. Index
  2. &{GET / 0x442250}
  3. ListStudents
  4. &{GET /students 0x4422f0}
  5. ViewStudent
  6. &{GET /students/:id 0x4427c0}
  7. UpdateStudent
  8. &{PUT /students/:id 0x443520}
  9. DeleteStudent
  10. &{DELETE /students/:id 0x443a30}
  11. CreateSchool
  12. &{POST /schools 0x444660}
  13. CreateStudent
  14. &{POST /students 0x442ed0}
  15. ListSchools
  16. &{GET /schools 0x443d50}
  17. ViewSchool
  18. &{GET /schools/:id 0x444200}
  19. UpdateSchool
  20. &{PUT /schools/:id 0x444cc0}
  21. DeleteSchool
  22. &{DELETE /schools/:id 0x4453b0}

我做错了什么?

英文:

I have a golang map like this:

  1. var routes map[string]*rest.Route

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

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

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

  1. 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.

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

Printing it out shows:

  1. Index
  2. &{GET / 0x442250}
  3. ListStudents
  4. &{GET /students 0x4422f0}
  5. ViewStudent
  6. &{GET /students/:id 0x4427c0}
  7. UpdateStudent
  8. &{PUT /students/:id 0x443520}
  9. DeleteStudent
  10. &{DELETE /students/:id 0x443a30}
  11. CreateSchool
  12. &{POST /schools 0x444660}
  13. CreateStudent
  14. &{POST /students 0x442ed0}
  15. ListSchools
  16. &{GET /schools 0x443d50}
  17. ViewSchool
  18. &{GET /schools/:id 0x444200}
  19. UpdateSchool
  20. &{PUT /schools/:id 0x444cc0}
  21. DeleteSchool
  22. &{DELETE /schools/:id 0x4453b0}

What am I doing wrong?

答案1

得分: 1

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

以下代码应该可以工作:

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

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

This should work:

  1. routes := []rest.Route{
  2. rest.Route{"GET", "/", handlers.GetIndex},
  3. rest.Route{"GET", "/students", handlers.ListStudents},
  4. // more routes
  5. }
  6. 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,我认为它更不容易混淆:

  1. api := rest.NewApi()
  2. api.Use(rest.DefaultDevStack...)
  3. router, err := rest.MakeRouter(
  4. // 在这里添加你的路由...
  5. )
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. api.SetApp(router)
  10. 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:

  1. api := rest.NewApi()
  2. api.Use(rest.DefaultDevStack...)
  3. router, err := rest.MakeRouter(
  4. // your routes here ...
  5. )
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. api.SetApp(router)
  10. 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:

确定