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

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

golang got empty slice after initialize

问题

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

type Queue struct {
    Name        string
    Concurrent  int
    Connections []*redis.Client
}

type Config struct {
    Queues    []Queue
    RedisAddr string
    RedisDB   int
}

type Tasker struct {
    Config Config
}

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

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

这是我的测试代码:

func main() {
    config := Config{
        RedisAddr: "10.1.1.59:6379",
        RedisDB:   8,
        Queues: []Queue{
            Queue{Name: "default", Concurrent: 1},
            Queue{Name: "mail", Concurrent: 2},
        },
    }
    daemon := Tasker{Config: config}
    daemon.StartListening()
}

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

英文:

I have 3 struct: Queue,Config,Tasker

type Queue struct {
  Name        string
  Concurrent  int
  Connections []*redis.Client
}

type Config struct {
  Queues    []Queue
  RedisAddr string
  RedisDB   int
}

type Tasker struct {
  Config Config
}

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

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

This is my test code

func main() {
  config := Config{
    RedisAddr: &quot;10.1.1.59:6379&quot;,
    RedisDB:   8,
    Queues: []Queue{
      Queue{Name: &quot;default&quot;, Concurrent: 1},
      Queue{Name: &quot;mail&quot;, Concurrent: 2},
    },
  }
  daemon := Tasker{Config: config}
  daemon.StartListening()

}

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

答案1

得分: 2

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

// ...

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

// ...

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

type Config struct {
  Queues    []*Queue
  RedisAddr string
  RedisDB   int
}

// ...

config := Config{
  RedisAddr: "10.1.1.59:6379",
  RedisDB:   8,
  Queues: []*Queue{
    &Queue{Name: "default", Concurrent: 1},
    &Queue{Name: "mail", Concurrent: 2},
  },
}

现在你的原始代码应该可以工作了

<details>
<summary>英文:</summary>

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:

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

Or if you want to use an auxillary variable, change `Config.Queues` type to `[]*Queue`:

    type Config struct {
      Queues    []*Queue
      RedisAddr string
      RedisDB   int
    }

    // ...

    config := Config{
      RedisAddr: &quot;10.1.1.59:6379&quot;,
      RedisDB:   8,
      Queues: []*Queue{
        &amp;Queue{Name: &quot;default&quot;, Concurrent: 1},
        &amp;Queue{Name: &quot;mail&quot;, Concurrent: 2},
      },
    }

Now your original code should work.

</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:

确定