「41」关于go中 _ [忽略]的使用

前序:

  • 前段时间看了这么个问题,输出是what?:
1
2
3
4
5
6
7
8
9
10
11
package main

func main() {
println(fff(1))
}

func fff(x int) (_, __ int) {
_, __ = x, x
return
}

结果分析

outpus:

0 1

汇编分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
▶ go tool compile -N -l -S signore.go 
"".main STEXT size=158 args=0x0 locals=0x40 funcid=0x0
0x0000 00000 (signore.go:3) TEXT "".main(SB), ABIInternal, $64-0
0x0000 00000 (signore.go:3) MOVQ (TLS), CX
0x0009 00009 (signore.go:3) CMPQ SP, 16(CX)
0x000d 00013 (signore.go:3) PCDATA $0, $-2
0x000d 00013 (signore.go:3) JLS 148
0x0013 00019 (signore.go:3) PCDATA $0, $-1
0x0013 00019 (signore.go:3) SUBQ $64, SP
0x0017 00023 (signore.go:3) MOVQ BP, 56(SP)
0x001c 00028 (signore.go:3) LEAQ 56(SP), BP
0x0021 00033 (signore.go:3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0021 00033 (signore.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0021 00033 (signore.go:4) MOVQ $1, (SP)
0x0029 00041 (signore.go:4) PCDATA $1, $0
0x0029 00041 (signore.go:4) CALL "".fff(SB)
0x002e 00046 (signore.go:4) MOVQ 8(SP), AX
0x0033 00051 (signore.go:4) MOVQ 16(SP), CX
0x0038 00056 (signore.go:4) MOVQ AX, ""..autotmp_2+32(SP)
0x003d 00061 (signore.go:4) MOVQ CX, ""..autotmp_3+24(SP)
0x0042 00066 (signore.go:4) MOVQ ""..autotmp_2+32(SP), AX
0x0047 00071 (signore.go:4) MOVQ AX, ""..autotmp_0+48(SP)
0x004c 00076 (signore.go:4) MOVQ ""..autotmp_3+24(SP), AX
0x0051 00081 (signore.go:4) MOVQ AX, ""..autotmp_1+40(SP)
0x0056 00086 (signore.go:4) CALL runtime.printlock(SB)
0x005b 00091 (signore.go:4) MOVQ ""..autotmp_0+48(SP), AX
0x0060 00096 (signore.go:4) MOVQ AX, (SP)
0x0064 00100 (signore.go:4) CALL runtime.printint(SB)
0x0069 00105 (signore.go:4) CALL runtime.printsp(SB)
0x006e 00110 (signore.go:4) MOVQ ""..autotmp_1+40(SP), AX
0x0073 00115 (signore.go:4) MOVQ AX, (SP)
0x0077 00119 (signore.go:4) CALL runtime.printint(SB)
0x007c 00124 (signore.go:4) NOP
0x0080 00128 (signore.go:4) CALL runtime.printnl(SB)
0x0085 00133 (signore.go:4) CALL runtime.printunlock(SB)
0x008a 00138 (signore.go:5) MOVQ 56(SP), BP
0x008f 00143 (signore.go:5) ADDQ $64, SP
0x0093 00147 (signore.go:5) RET
0x0094 00148 (signore.go:5) NOP
0x0094 00148 (signore.go:3) PCDATA $1, $-1
0x0094 00148 (signore.go:3) PCDATA $0, $-2
0x0094 00148 (signore.go:3) CALL runtime.morestack_noctxt(SB)
0x0099 00153 (signore.go:3) PCDATA $0, $-1
0x0099 00153 (signore.go:3) JMP 0
0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 81 eH..%....H;a....
0x0010 00 00 00 48 83 ec 40 48 89 6c 24 38 48 8d 6c 24 ...H..@H.l$8H.l$
0x0020 38 48 c7 04 24 01 00 00 00 e8 00 00 00 00 48 8b 8H..$.........H.
0x0030 44 24 08 48 8b 4c 24 10 48 89 44 24 20 48 89 4c D$.H.L$.H.D$ H.L
0x0040 24 18 48 8b 44 24 20 48 89 44 24 30 48 8b 44 24 $.H.D$ H.D$0H.D$
0x0050 18 48 89 44 24 28 e8 00 00 00 00 48 8b 44 24 30 .H.D$(.....H.D$0
0x0060 48 89 04 24 e8 00 00 00 00 e8 00 00 00 00 48 8b H..$..........H.
0x0070 44 24 28 48 89 04 24 e8 00 00 00 00 0f 1f 40 00 D$(H..$.......@.
0x0080 e8 00 00 00 00 e8 00 00 00 00 48 8b 6c 24 38 48 ..........H.l$8H
0x0090 83 c4 40 c3 e8 00 00 00 00 e9 62 ff ff ff ..@.......b...
rel 5+4 t=17 TLS+0
rel 42+4 t=8 "".fff+0
rel 87+4 t=8 runtime.printlock+0
rel 101+4 t=8 runtime.printint+0
rel 106+4 t=8 runtime.printsp+0
rel 120+4 t=8 runtime.printint+0
rel 129+4 t=8 runtime.printnl+0
rel 134+4 t=8 runtime.printunlock+0
rel 149+4 t=8 runtime.morestack_noctxt+0
"".fff STEXT nosplit size=29 args=0x18 locals=0x0 funcid=0x0
0x0000 00000 (signore.go:7) TEXT "".fff(SB), NOSPLIT|ABIInternal, $0-24
0x0000 00000 (signore.go:7) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (signore.go:7) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
// ***这个 [~b]很关键,划重点
0x0000 00000 (signore.go:7) MOVQ $0, "".~b1+16(SP)
0x0009 00009 (signore.go:7) MOVQ $0, "".__+24(SP)
0x0012 00018 (signore.go:8) MOVQ "".x+8(SP), AX
0x0017 00023 (signore.go:8) MOVQ AX, "".__+24(SP)
0x001c 00028 (signore.go:9) RET
0x0000 48 c7 44 24 10 00 00 00 00 48 c7 44 24 18 00 00 H.D$.....H.D$...
0x0010 00 00 48 8b 44 24 08 48 89 44 24 18 c3 ..H.D$.H.D$..
go.cuinfo.packagename. SDWARFCUINFO dupok size=0
0x0000 6d 61 69 6e main
""..inittask SNOPTRDATA size=24
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0010 00 00 00 00 00 00 00 00 ........
gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8
0x0000 01 00 00 00 00 00 00 00 ........

为何 _ 就可以忽略返回值呢?

官方文档

调用方:

  • dcl 「参数」
  • import 「导入」

再向上:

–>genhash

再向上:

—>compile

start:

—>gc.main

有意思的点: gc.main需要9步,编译在第8步了,前面的有兴趣可自己看看.

End