dongzanghua8422 2017-03-18 16:11
浏览 24
已采纳

通道变量初始化后创建缓冲的通道

I can initialise a buffered string channel like this

queue := make(chan string, 10)

But how to initialise a buffered channel in a struct in Go ? Basically I want to allocate memory to a buffered string channel. But initially in the struct I would just define it and in struct initialisation, I would like to allocate memory to it

type message struct {

  queue *chan string
// or will it be 
//queue []chan string

}

func (this *message) init() {

  queue = make(chan string,10)

  this.queue = &queue

}
  • 写回答

2条回答 默认 最新

  • dtdr57046 2017-03-18 16:17
    关注

    Do this:

    type message struct {
       queue chan string
    }
    
    func (m *message) init() {
        m.queue = make(chan string, 10)
    }
    

    There's no need to take the address of the channel in this scenario.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题