Skip to content

Instantly share code, notes, and snippets.

@mohae
Created February 9, 2017 01:28
Show Gist options
  • Save mohae/60cfb241cdc1fa6b163550ca55586c99 to your computer and use it in GitHub Desktop.
Save mohae/60cfb241cdc1fa6b163550ca55586c99 to your computer and use it in GitHub Desktop.
go: method vs func
package ex
type Foo struct {
Val string
}
func (f *Foo) Bar() string {
return f.Val
}
func Bar(f *Foo) string {
return f.Val
}
go tool compile -s ex.go
"".(*Foo).Bar t=1 size=23 args=0x18 locals=0x0
0x0000 00000 (ex.go:7) TEXT "".(*Foo).Bar(SB), $0-24
0x0000 00000 (ex.go:7) NOP
0x0000 00000 (ex.go:7) NOP
0x0000 00000 (ex.go:7) FUNCDATA $0, gclocals·0b86ef39f3fed835f14ba5f4d7c62fa2(SB)
0x0000 00000 (ex.go:7) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (ex.go:8) MOVQ "".f+8(FP), AX
0x0005 00005 (ex.go:8) MOVQ (AX), CX
0x0008 00008 (ex.go:8) MOVQ 8(AX), DX
0x000c 00012 (ex.go:8) MOVQ CX, "".~r0+16(FP)
0x0011 00017 (ex.go:8) MOVQ DX, "".~r0+24(FP)
0x0016 00022 (ex.go:8) RET
0x0000 48 8b 44 24 08 48 8b 08 48 8b 50 08 48 89 4c 24 H.D$.H..H.P.H.L$
0x0010 10 48 89 54 24 18 c3 .H.T$..
"".Bar t=1 size=23 args=0x18 locals=0x0
0x0000 00000 (ex.go:11) TEXT "".Bar(SB), $0-24
0x0000 00000 (ex.go:11) NOP
0x0000 00000 (ex.go:11) NOP
0x0000 00000 (ex.go:11) FUNCDATA $0, gclocals·0b86ef39f3fed835f14ba5f4d7c62fa2(SB)
0x0000 00000 (ex.go:11) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (ex.go:12) MOVQ "".f+8(FP), AX
0x0005 00005 (ex.go:12) MOVQ (AX), CX
0x0008 00008 (ex.go:12) MOVQ 8(AX), DX
0x000c 00012 (ex.go:12) MOVQ CX, "".~r1+16(FP)
0x0011 00017 (ex.go:12) MOVQ DX, "".~r1+24(FP)
0x0016 00022 (ex.go:12) RET
0x0000 48 8b 44 24 08 48 8b 08 48 8b 50 08 48 89 4c 24 H.D$.H..H.P.H.L$
0x0010 10 48 89 54 24 18 c3 .H.T$..
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=9 dupok size=8
0x0000 01 00 00 00 00 00 00 00 ........
gclocals·0b86ef39f3fed835f14ba5f4d7c62fa2 t=9 dupok size=12
0x0000 01 00 00 00 03 00 00 00 01 00 00 00 ............
"".(*Foo).Bar·f t=9 dupok size=8
0x0000 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 "".(*Foo).Bar+0
"".Bar·f t=9 dupok size=8
0x0000 00 00 00 00 00 00 00 00 ........
rel 0+8 t=1 "".Bar+0
runtime.gcbits.01 t=9 dupok size=1
0x0000 01 .
type..namedata.*ex.Foo. t=9 dupok size=10
0x0000 01 00 07 2a 65 78 2e 46 6f 6f ...*ex.Foo
type..namedata.Val. t=9 dupok size=6
0x0000 01 00 03 56 61 6c ...Val
type."".Foo t=9 size=120
0x0000 10 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 45 d6 a9 80 07 08 08 19 00 00 00 00 00 00 00 00 E...............
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0040 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................
0x0050 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 ........(.......
0x0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0070 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.algarray+112
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*ex.Foo.+0
rel 44+4 t=5 type.*"".Foo+0
rel 48+8 t=1 type..importpath."".+0
rel 56+8 t=1 type."".Foo+96
rel 80+4 t=5 type..importpath."".+0
rel 96+8 t=1 type..namedata.Val.+0
rel 104+8 t=1 type.string+0
type..namedata.*func(*ex.Foo) string. t=9 dupok size=24
0x0000 00 00 15 2a 66 75 6e 63 28 2a 65 78 2e 46 6f 6f ...*func(*ex.Foo
0x0010 29 20 73 74 72 69 6e 67 ) string
type.func(*"".Foo) string t=9 dupok size=72
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 bb c4 69 76 02 08 08 33 00 00 00 00 00 00 00 00 ..iv...3........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0040 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.algarray+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*func(*ex.Foo) string.+0
rel 56+8 t=1 type.*"".Foo+0
rel 64+8 t=1 type.string+0
go.typelink.func(*"".Foo) string t=9 dupok size=4
0x0000 00 00 00 00 ....
rel 0+4 t=5 type.func(*"".Foo) string+0
type..namedata.Bar. t=9 dupok size=6
0x0000 01 00 03 42 61 72 ...Bar
type..namedata.*func() string. t=9 dupok size=17
0x0000 00 00 0e 2a 66 75 6e 63 28 29 20 73 74 72 69 6e ...*func() strin
0x0010 67 g
type.func() string t=9 dupok size=64
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 a2 6d cb 1e 02 08 08 33 00 00 00 00 00 00 00 00 .m.....3........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
rel 24+8 t=1 runtime.algarray+0
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*func() string.+0
rel 56+8 t=1 type.string+0
go.typelink.func() string t=9 dupok size=4
0x0000 00 00 00 00 ....
rel 0+4 t=5 type.func() string+0
type.*"".Foo t=9 size=88
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 ................
0x0010 94 70 b7 c5 01 08 08 36 00 00 00 00 00 00 00 00 .p.....6........
0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0030 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ................
0x0040 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0050 00 00 00 00 00 00 00 00 ........
rel 24+8 t=1 runtime.algarray+80
rel 32+8 t=1 runtime.gcbits.01+0
rel 40+4 t=5 type..namedata.*ex.Foo.+0
rel 48+8 t=1 type."".Foo+0
rel 56+4 t=5 type..importpath."".+0
rel 72+4 t=5 type..namedata.Bar.+0
rel 76+4 t=23 type.func() string+0
rel 80+4 t=23 "".(*Foo).Bar+0
rel 84+4 t=23 "".(*Foo).Bar+0
go.typelink.*"".Foo t=9 size=4
0x0000 00 00 00 00 ....
rel 0+4 t=5 type.*"".Foo+0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment