package main import "time"
func main() { s := []int{100, 200} println(&s) go func() { s[0] = 300 s = []int{300, 400} }() time.Sleep(1 * time.Second) } just curious about this ,can you help explain the assemble code here "".main t=1 size=241 args=0x0 locals=0x28 0x0000 00000 (main.go:5) TEXT "".main(SB), $40-0 0x0000 00000 (main.go:5) MOVQ (TLS), CX 0x0009 00009 (main.go:5) CMPQ SP, 16(CX) 0x000d 00013 (main.go:5) JLS 231 0x0013 00019 (main.go:5) SUBQ $40, SP 0x0017 00023 (main.go:5) MOVQ BP, 32(SP) 0x001c 00028 (main.go:5) LEAQ 32(SP), BP 0x0021 00033 (main.go:5) FUNCDATA $0, gclocals·69c1753bd5f81501d95132d08af04464(SB) 0x0021 00033 (main.go:5) FUNCDATA $1, gclocals·0c8aa8e80191a30eac23f1a218103f16(SB) 0x0021 00033 (main.go:6) LEAQ type.[]int(SB), AX 0x0028 00040 (main.go:6) MOVQ AX, (SP) 0x002c 00044 (main.go:6) PCDATA $0, $0 0x002c 00044 (main.go:6) CALL runtime.newobject(SB) 0x0031 00049 (main.go:6) MOVQ 8(SP), AX 0x0036 00054 (main.go:6) MOVQ AX, "".&s+24(SP) 0x003b 00059 (main.go:6) LEAQ type.[2]int(SB), CX 0x0042 00066 (main.go:6) MOVQ CX, (SP) 0x0046 00070 (main.go:6) PCDATA $0, $1 0x0046 00070 (main.go:6) CALL runtime.newobject(SB) 0x004b 00075 (main.go:6) MOVQ 8(SP), AX 0x0050 00080 (main.go:6) MOVUPS "".statictmp_2(SB), X0 0x0057 00087 (main.go:6) MOVUPS X0, (AX) 0x005a 00090 (main.go:6) MOVQ "".&s+24(SP), CX 0x005f 00095 (main.go:6) MOVQ $2, 8(CX) 0x0067 00103 (main.go:6) MOVQ $2, 16(CX) 0x006f 00111 (main.go:6) MOVL runtime.writeBarrier(SB), DX 0x0075 00117 (main.go:6) TESTB DL, DL 0x0077 00119 (main.go:6) JNE $0, 210 0x0079 00121 (main.go:6) MOVQ AX, (CX) 0x007c 00124 (main.go:7) PCDATA $0, $1 0x007c 00124 (main.go:7) CALL runtime.printlock(SB) 0x0081 00129 (main.go:7) MOVQ "".&s+24(SP), AX 0x0086 00134 (main.go:7) MOVQ AX, (SP) 0x008a 00138 (main.go:7) PCDATA $0, $1 0x008a 00138 (main.go:7) CALL runtime.printpointer(SB) 0x008f 00143 (main.go:7) PCDATA $0, $1 0x008f 00143 (main.go:7) CALL runtime.printnl(SB) 0x0094 00148 (main.go:7) PCDATA $0, $1 0x0094 00148 (main.go:7) CALL runtime.printunlock(SB) 0x0099 00153 (main.go:11) MOVQ "".&s+24(SP), AX 0x009e 00158 (main.go:11) MOVQ AX, 16(SP) 0x00a3 00163 (main.go:11) MOVL $8, (SP) 0x00aa 00170 (main.go:11) LEAQ "".main.func1·f(SB), AX 0x00b1 00177 (main.go:11) MOVQ AX, 8(SP) 0x00b6 00182 (main.go:11) PCDATA $0, $0 0x00b6 00182 (main.go:11) CALL runtime.newproc(SB) 0x00bb 00187 (main.go:12) MOVQ $1000000000, (SP) 0x00c3 00195 (main.go:12) PCDATA $0, $0 0x00c3 00195 (main.go:12) CALL time.Sleep(SB) 0x00c8 00200 (main.go:13) MOVQ 32(SP), BP 0x00cd 00205 (main.go:13) ADDQ $40, SP 0x00d1 00209 (main.go:13) RET 0x00d2 00210 (main.go:6) MOVQ CX, (SP) 0x00d6 00214 (main.go:6) MOVQ AX, 8(SP) 0x00db 00219 (main.go:6) PCDATA $0, $1 0x00db 00219 (main.go:6) CALL runtime.writebarrierptr(SB) 0x00e0 00224 (main.go:6) MOVQ "".&s+24(SP), CX 0x00e5 00229 (main.go:7) JMP 124 0x00e7 00231 (main.go:7) NOP 0x00e7 00231 (main.go:5) CALL runtime.morestack_noctxt(SB) 0x00ec 00236 (main.go:5) JMP 0 在 2016年11月3日星期四 UTC+8下午2:56:10,Ian Lance Taylor写道: > > On Wed, Nov 2, 2016 at 11:48 PM, 刘桂祥 <liuguix...@gmail.com <javascript:>> > wrote: > > I just want to look what variable datastructure is passed to the closure > > func but I am confused with the assemble code > > > > > > // example.go > > > > package main > > > > > > import "time" > > > > > > func main() { > > > > s := []int{100, 200} > > > > func() { > > > > s[0] = 300 > > > > s = []int{300, 400} > > > > }() > > > > time.Sleep(1 * time.Second) > > > > } > > In this example you are calling the function directly, so the compiler > doesn't use a closure at all. It simply passes the closure values as > arguments to the function. There is no need for an actual closure > when you always call the function. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.