gpt4 book ai didi

go - 在 channel 变量初始化后创建缓冲 channel

转载 作者:IT王子 更新时间:2023-10-29 02:32:43 28 4
gpt4 key购买 nike

我可以像这样初始化一个缓冲字符串 channel

queue := make(chan string, 10)

但是如何在 Go 中的结构中初始化缓冲 channel ?基本上我想将内存分配给缓冲的字符串 channel 。但最初在结构中我只是定义它,在结构初始化中,我想为它分配内存

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

}

最佳答案

这样做:

type message struct {
queue chan string
}

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

在这种情况下不需要获取 channel 的地址。

关于go - 在 channel 变量初始化后创建缓冲 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876720/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com