从 goroutine 返回值会发生什么

有人可以提供从 goroutine 返回的澄清值。goroutine 的返回值是否存储在权益上。


例子 :


// function getNumber returns the "int i" and we can't use this returned value

// because this function is invoked as goroutine.

// We know that, to communicate between main and goroutine one could

// use the channel ( chan <- i), but I am interested to know about

// the use of return i in goroutine. Is it possible to get/ use this 

// returned value.

func getNumber(i int) int {

    return i   

    }


func main() {


    for i:=0; i<10; i++ {

        go printNumber(i)

        }

    time.Sleep(5)

    }

我们应该尽量避免 goroutine 中的返回值吗?


catspeake
浏览 293回答 3
3回答

慕森王

快速查看汇编输出显示$ go build -gcflags -S z.go该getNumber()函数确实将其结果存储到堆栈中"".getNumber t=1 size=16 value=0 args=0x10 locals=0x0&nbsp; &nbsp; 0x0000 00000 (z.go:5)&nbsp; &nbsp;TEXT&nbsp; &nbsp; "".getNumber+0(SB),4,$0-16&nbsp; &nbsp; 0x0000 00000 (z.go:6)&nbsp; &nbsp;MOVQ&nbsp; &nbsp; "".i+8(FP),BX&nbsp; &nbsp; 0x0005 00005 (z.go:6)&nbsp; &nbsp;MOVQ&nbsp; &nbsp; BX,"".~r1+16(FP)&nbsp; &nbsp; 0x000a 00010 (z.go:6)&nbsp; &nbsp;RET ,所以当它被 goroutine 调用时,它确实将其结果存储到堆栈中。然而,这是一个新的堆栈,它在 goroutine 结束时被销毁,因此无法检索返回值。"".main t=1 size=96 value=0 args=0x0 locals=0x18&nbsp; &nbsp; 0x0000 00000 (z.go:9)&nbsp; &nbsp;TEXT&nbsp; &nbsp; "".main+0(SB),$24-0&nbsp; &nbsp; 0x0000 00000 (z.go:9)&nbsp; &nbsp;MOVQ&nbsp; &nbsp; (TLS),CX&nbsp; &nbsp; 0x0009 00009 (z.go:9)&nbsp; &nbsp;CMPQ&nbsp; &nbsp; SP,16(CX)&nbsp; &nbsp; 0x000d 00013 (z.go:9)&nbsp; &nbsp;JHI ,22&nbsp; &nbsp; 0x000f 00015 (z.go:9)&nbsp; &nbsp;CALL&nbsp; &nbsp; ,runtime.morestack_noctxt(SB)&nbsp; &nbsp; 0x0014 00020 (z.go:9)&nbsp; &nbsp;JMP ,0&nbsp; &nbsp; 0x0016 00022 (z.go:9)&nbsp; &nbsp;SUBQ&nbsp; &nbsp; $24,SP&nbsp; &nbsp; 0x001a 00026 (z.go:10)&nbsp; MOVQ&nbsp; &nbsp; $0,AX&nbsp; &nbsp; 0x001c 00028 (z.go:10)&nbsp; CMPQ&nbsp; &nbsp; AX,$10&nbsp; &nbsp; 0x0020 00032 (z.go:10)&nbsp; JGE $0,74&nbsp; &nbsp; 0x0022 00034 (z.go:11)&nbsp; MOVQ&nbsp; &nbsp; AX,"".i+16(SP)&nbsp; &nbsp; 0x0027 00039 (z.go:11)&nbsp; MOVQ&nbsp; &nbsp; AX,(SP)&nbsp; &nbsp; 0x002b 00043 (z.go:11)&nbsp; MOVQ&nbsp; &nbsp; $"".getNumber·f+0(SB),CX&nbsp; &nbsp; 0x0032 00050 (z.go:11)&nbsp; PUSHQ&nbsp; &nbsp;CX,&nbsp; &nbsp; 0x0033 00051 (z.go:11)&nbsp; PUSHQ&nbsp; &nbsp;$16,&nbsp; &nbsp; 0x0035 00053 (z.go:11)&nbsp; PCDATA&nbsp; $0,$0&nbsp; &nbsp; 0x0035 00053 (z.go:11)&nbsp; CALL&nbsp; &nbsp; ,runtime.newproc(SB)&nbsp; &nbsp; 0x003a 00058 (z.go:11)&nbsp; POPQ&nbsp; &nbsp; ,CX&nbsp; &nbsp; 0x003b 00059 (z.go:11)&nbsp; POPQ&nbsp; &nbsp; ,CX&nbsp; &nbsp; 0x003c 00060 (z.go:10)&nbsp; MOVQ&nbsp; &nbsp; "".i+16(SP),AX&nbsp; &nbsp; 0x0041 00065 (z.go:10)&nbsp; INCQ&nbsp; &nbsp; ,AX&nbsp; &nbsp; 0x0044 00068 (z.go:10)&nbsp; NOP ,&nbsp; &nbsp; 0x0044 00068 (z.go:10)&nbsp; CMPQ&nbsp; &nbsp; AX,$10&nbsp; &nbsp; 0x0048 00072 (z.go:10)&nbsp; JLT $0,34&nbsp; &nbsp; 0x004a 00074 (z.go:13)&nbsp; MOVQ&nbsp; &nbsp; $5,(SP)&nbsp; &nbsp; 0x0052 00082 (z.go:13)&nbsp; PCDATA&nbsp; $0,$0&nbsp; &nbsp; 0x0052 00082 (z.go:13)&nbsp; CALL&nbsp; &nbsp; ,time.Sleep(SB)&nbsp; &nbsp; 0x0057 00087 (z.go:14)&nbsp; ADDQ&nbsp; &nbsp; $24,SP&nbsp; &nbsp; 0x005b 00091 (z.go:14)&nbsp; RET ,然而,没有办法检索这些结果。

素胚勾勒不出你

引用Go 语言规范:Go 语句:如果函数有任何返回值,它们会在函数完成时被丢弃。因此允许将返回值作为 goroutine 执行函数 - 它没有任何问题,并且规范明确指出它们的返回值被简单地丢弃,它不会导致任何错误,但您不会以通常的方式得到它(就像您直接调用该函数一样)。

忽然笑

值被丢弃。并没有什么特别的go声明。你也可以写..._ = getNumber(i)...要不就...getNumber(i)...甚至
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go