1

goroutine 传指针的问题

 2 years ago
source link: https://www.v2ex.com/t/807900
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
goroutine 传指针的问题

V2EX  ›  Go 编程语言

goroutine 传指针的问题

  iyear · 8 小时 31 分钟前 · 353 次点击
for {
		select {
		case msg, ok := <-msgChan:
			if !ok {
				return
			}
			limit <- struct{}{} 
			go func(t *Message) {
				send(t)
				<-limit
			}(msg)
		}
	}

msg 是*Message

这样的传参会导致 t 被下次循环的 msg 覆盖吗 (其实可以不这么控制并发,如果只能这么写的话

4 条回复    2021-10-15 01:33:52 +08:00

thet

thet   8 小时 21 分钟前 via iPhone

ch2

ch2   7 小时 47 分钟前

:= 出来是一个新的,下一次循环跟上一次没关系

rrfeng

rrfeng   5 小时 34 分钟前 via Android

这个 select 么有用,可以删掉。

rrfeng

rrfeng   5 小时 32 分钟前 via Android

这里的变量 t 没问题的,但是要小心你其他地方的程序同时操作指针所指向的对象,可能会有意想不到的后果。

关于   ·   帮助文档   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   1137 人在线   最高记录 5497   ·  

创意工作者们的社区

World is powered by solitude

VERSION: 3.9.8.5 · 17ms · UTC 23:06 · PVG 07:06 · LAX 16:06 · JFK 19:06
♥ Do have faith in what you're doing.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK