在初始化后,Golang得到了一个空的切片。

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

golang got empty slice after initialize

问题

我有3个结构体:Queue、Config和Tasker。

  1. type Queue struct {
  2. Name string
  3. Concurrent int
  4. Connections []*redis.Client
  5. }
  6. type Config struct {
  7. Queues []Queue
  8. RedisAddr string
  9. RedisDB int
  10. }
  11. type Tasker struct {
  12. Config Config
  13. }

问题出现在这个方法中,我在for循环中初始化了queue.Connections,但是在for循环外部,我得到了queue.Connections的长度为0。

  1. func (t *Tasker) StartListening() {
  2. for j := 0; j < len(t.Config.Queues); j++ {
  3. queue := t.Config.Queues[j]
  4. queue.Connections = make([]*redis.Client, queue.Concurrent)
  5. fmt.Println(len(queue.Connections)) // 这里打印了正确的长度,对于默认队列是1,对于邮件队列是2
  6. }
  7. fmt.Println(len(t.Config.Queues[0].Connections)) // 但是为什么这里打印的是0?
  8. }

这是我的测试代码:

  1. func main() {
  2. config := Config{
  3. RedisAddr: "10.1.1.59:6379",
  4. RedisDB: 8,
  5. Queues: []Queue{
  6. Queue{Name: "default", Concurrent: 1},
  7. Queue{Name: "mail", Concurrent: 2},
  8. },
  9. }
  10. daemon := Tasker{Config: config}
  11. daemon.StartListening()
  12. }

为什么在for循环外部fmt.Println(len(t.Config.Queues[0].Connections))的结果是0?

英文:

I have 3 struct: Queue,Config,Tasker

  1. type Queue struct {
  2. Name string
  3. Concurrent int
  4. Connections []*redis.Client
  5. }
  6. type Config struct {
  7. Queues []Queue
  8. RedisAddr string
  9. RedisDB int
  10. }
  11. type Tasker struct {
  12. Config Config
  13. }

The problem happend in this method, I initialize queue.Connections in for-loop, but I got zero length of queue.Connections outside the for-loop

  1. func (t *Tasker) StartListening() {
  2. for j := 0; j &lt; len(t.Config.Queues); j++ {
  3. queue := t.Config.Queues[j]
  4. queue.Connections = make([]*redis.Client, queue.Concurrent)
  5. fmt.Println(len(queue.Connections)) //here print correct length, 1 for default queue, 2 for mail queue
  6. }
  7. fmt.Println(len(t.Config.Queues[0].Connections)) //but why here print 0?
  8. }

This is my test code

  1. func main() {
  2. config := Config{
  3. RedisAddr: &quot;10.1.1.59:6379&quot;,
  4. RedisDB: 8,
  5. Queues: []Queue{
  6. Queue{Name: &quot;default&quot;, Concurrent: 1},
  7. Queue{Name: &quot;mail&quot;, Concurrent: 2},
  8. },
  9. }
  10. daemon := Tasker{Config: config}
  11. daemon.StartListening()
  12. }

why fmt.Println(len(t.Config.Queues[0].Connections)) is 0 outside the for-loop?

答案1

得分: 2

你正在创建一个新的Queue而不是访问Config结构中的Queue,这个新值会阻止对Config.Queues中的Queue进行修改。尝试直接赋值:

  1. // ...
  2. t.Config.Queues[j].Connections = make([]*redis.Client, queue.Concurrent)
  3. // ...

或者如果你想使用辅助变量,将Config.Queues的类型更改为[]*Queue

  1. type Config struct {
  2. Queues []*Queue
  3. RedisAddr string
  4. RedisDB int
  5. }
  6. // ...
  7. config := Config{
  8. RedisAddr: "10.1.1.59:6379",
  9. RedisDB: 8,
  10. Queues: []*Queue{
  11. &Queue{Name: "default", Concurrent: 1},
  12. &Queue{Name: "mail", Concurrent: 2},
  13. },
  14. }
  15. 现在你的原始代码应该可以工作了
  16. <details>
  17. <summary>英文:</summary>
  18. You are creating a new `Queue` instead of accessing the one in the `Config` structure, and this new value prevents modification to the `Queue` in `Config.Queues`. Try direct assignment:
  19. // ...
  20. t.Config.Queues[j].Connections = make([]*redis.Client, queue.Concurrent)
  21. // ...
  22. Or if you want to use an auxillary variable, change `Config.Queues` type to `[]*Queue`:
  23. type Config struct {
  24. Queues []*Queue
  25. RedisAddr string
  26. RedisDB int
  27. }
  28. // ...
  29. config := Config{
  30. RedisAddr: &quot;10.1.1.59:6379&quot;,
  31. RedisDB: 8,
  32. Queues: []*Queue{
  33. &amp;Queue{Name: &quot;default&quot;, Concurrent: 1},
  34. &amp;Queue{Name: &quot;mail&quot;, Concurrent: 2},
  35. },
  36. }
  37. Now your original code should work.
  38. </details>

huangapple
  • 本文由 发表于 2017年6月16日 17:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/44586280.html
匿名

发表评论

匿名网友

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

确定