string("hello")初始化过如何初始化的呢

来源:4-2 数组,字符串,切片底层是一样的吗?(一)

煎鱼侠

2023-11-08

string(“hello”)初始化过程是怎么样的,入口函数是那个,经过几个函数最终完成初始化的呢?汇编里也没有第六行的内容

图片描述

PS D:\workspace\goProject\gotest\string> go build -gcflags -S main.go

command-line-arguments

main.main STEXT size=103 args=0x0 locals=0x40 funcid=0x0 align=0x0
0x0000 00000 (D:/workspace/goProject/gotest/string/main.go:5) TEXT main.main(SB), ABIInternal, $64-0
0x0000 00000 (D:/workspace/goProject/gotest/string/main.go:5) CMPQ SP, 16(R14)
0x0004 00004 (D:/workspace/goProject/gotest/string/main.go:5) PCDATA $0, $-2
0x0004 00004 (D:/workspace/goProject/gotest/string/main.go:5) JLS 92
0x0006 00006 (D:/workspace/goProject/gotest/string/main.go:5) PCDATA $0, $-1
0x0006 00006 (D:/workspace/goProject/gotest/string/main.go:5) PUSHQ BP
0x0007 00007 (D:/workspace/goProject/gotest/string/main.go:5) MOVQ SP, BP
0x000a 00010 (D:/workspace/goProject/gotest/string/main.go:5) SUBQ $56, SP
0x000e 00014 (D:/workspace/goProject/gotest/string/main.go:5) FUNCDATA $0, gclocals·J5F+7Qw7O7ve2QcWC7DpeQ==(SB)
0x000e 00014 (D:/workspace/goProject/gotest/string/main.go:5) FUNCDATA $1, gclocals·5aa34RaZcmo0NkRpBHp2fg==(SB)
0x000e 00014 (D:/workspace/goProject/gotest/string/main.go:5) FUNCDATA $2, main.main.stkobj(SB)
0x000e 00014 (D:/workspace/goProject/gotest/string/main.go:7) MOVUPS X15, main…autotmp_9+40(SP)
0x0014 00020 (D:/workspace/goProject/gotest/string/main.go:7) LEAQ go:string.“123”(SB), AX
0x001b 00027 (D:/workspace/goProject/gotest/string/main.go:7) MOVL $3, BX
0x0020 00032 (D:/workspace/goProject/gotest/string/main.go:7) PCDATA $1, $1
0x0020 00032 (D:/workspace/goProject/gotest/string/main.go:7) CALL runtime.convTstring(SB)
0x0025 00037 (D:/workspace/goProject/gotest/string/main.go:7) LEAQ type:string(SB), CX
0x002c 00044 (D:/workspace/goProject/gotest/string/main.go:7) MOVQ CX, main…autotmp_9+40(SP)
0x0031 00049 (D:/workspace/goProject/gotest/string/main.go:7) MOVQ AX, main…autotmp_9+48(SP)
0x0036 00054 (D:\install\go\src\fmt\print.go:314) MOVQ os.Stdout(SB), BX
0x003d 00061 () NOP
0x003d 00061 (D:\install\go\src\fmt\print.go:314) LEAQ go:itab.os.File,io.Writer(SB), AX
0x0044 00068 (D:\install\go\src\fmt\print.go:314) LEAQ main…autotmp_9+40(SP), CX
0x0049 00073 (D:\install\go\src\fmt\print.go:314) MOVL $1, DI
0x004e 00078 (D:\install\go\src\fmt\print.go:314) MOVQ DI, SI
0x0051 00081 (D:\install\go\src\fmt\print.go:314) PCDATA $1, $0
0x0051 00081 (D:\install\go\src\fmt\print.go:314) CALL fmt.Fprintln(SB)
0x0056 00086 (D:/workspace/goProject/gotest/string/main.go:8) ADDQ $56, SP
0x005a 00090 (D:/workspace/goProject/gotest/string/main.go:8) POPQ BP
0x005b 00091 (D:/workspace/goProject/gotest/string/main.go:8) RET
0x005c 00092 (D:/workspace/goProject/gotest/string/main.go:8) NOP
0x005c 00092 (D:/workspace/goProject/gotest/string/main.go:5) PCDATA $1, $-1
0x005c 00092 (D:/workspace/goProject/gotest/string/main.go:5) PCDATA $0, $-2
0x005c 00092 (D:/workspace/goProject/gotest/string/main.go:5) NOP
0x0060 00096 (D:/workspace/goProject/gotest/string/main.go:5) CALL runtime.morestack_noctxt(SB)
0x0065 00101 (D:/workspace/goProject/gotest/string/main.go:5) PCDATA $0, −10x006500101(D:/workspace/goProject/gotest/string/main.go:5)JMP00x0000493b66107656554889e54883ec38440fI;f.vVUH..H..8D.0x0010117c2428488d0500000000bb03000000.∣-1 0x0065 00101 (D:/workspace/goProject/gotest/string/main.go:5) JMP 0 0x0000 49 3b 66 10 76 56 55 48 89 e5 48 83 ec 38 44 0f I;f.vVUH..H..8D. 0x0010 11 7c 24 28 48 8d 05 00 00 00 00 bb 03 00 00 00 .|10x006500101(D:/workspace/goProject/gotest/string/main.go:5)JMP00x0000493b66107656554889e54883ec38440fI;f.vVUH..H..8D.0x0010117c2428488d0500000000bb03000000.(H…
0x0020 e8 00 00 00 00 48 8d 0d 00 00 00 00 48 89 4c 24 …H…H.L$
0x0030 28 48 89 44 24 30 48 8b 1d 00 00 00 00 48 8d 05 (H.D0H......H..0x004000000000488d4c2428bf010000004889....H.L0H......H.. 0x0040 00 00 00 00 48 8d 4c 24 28 bf 01 00 00 00 48 89 ....H.L0H......H..0x004000000000488d4c2428bf010000004889....H.L(…H.
0x0050 fe e8 00 00 00 00 48 83 c4 38 5d c3 0f 1f 40 00 …H…8]…@.
0x0060 e8 00 00 00 00 eb 99 …
rel 2+0 t=23 type:string+0
rel 2+0 t=23 type:os.File+0
rel 23+4 t=14 go:string.“123”+0
rel 33+4 t=7 runtime.convTstring+0
rel 40+4 t=14 type:string+0
rel 57+4 t=14 os.Stdout+0
rel 64+4 t=14 go:itab.os.File,io.Writer+0
rel 82+4 t=7 fmt.Fprintln+0
rel 97+4 t=7 runtime.morestack_noctxt+0
go:cuinfo.producer.main SDWARFCUINFO dupok size=0
0x0000 72 65 67 61 62 69 regabi
go:cuinfo.packagename.main SDWARFCUINFO dupok size=0
0x0000 6d 61 69 6e main
go:info.fmt.Println$abstract SDWARFABSFCN dupok size=44
0x0000 05 66 6d 74 2e 50 72 69 6e 74 6c 6e 00 01 b9 02 .fmt.Println…
0x0010 01 13 61 00 00 00 00 00 00 13 6e 00 01 00 00 00 …a…n…
0x0020 00 13 65 72 72 00 01 00 00 00 00 00 …err…
rel 0+0 t=22 type:[]interface {}+0
rel 0+0 t=22 type:error+0
rel 0+0 t=22 type:int+0
rel 21+4 t=31 go:info.[]interface {}+0
rel 29+4 t=31 go:info.int+0
rel 39+4 t=31 go:info.error+0
go:itab.os.File,io.Writer SRODATA dupok size=32
0x0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 …
0x0010 5a 22 ee 60 00 00 00 00 00 00 00 00 00 00 00 00 Z".`…
rel 0+8 t=1 type:io.Writer+0
rel 8+8 t=1 type:os.File+0
rel 24+8 t=-32767 os.(File).Write+0
main…inittask SNOPTRDATA size=8
0x0000 00 00 00 00 00 00 00 00 …
rel 0+0 t=81 fmt…inittask+0
go:string.“123” SRODATA dupok size=3
0x0000 31 32 33 123
runtime.nilinterequal·f SRODATA dupok size=8
0x0000 00 00 00 00 00 00 00 00 …
rel 0+8 t=1 runtime.nilinterequal+0
runtime.memequal64·f SRODATA dupok size=8
0x0000 00 00 00 00 00 00 00 00 …
rel 0+8 t=1 runtime.memequal64+0
runtime.gcbits.0100000000000000 SRODATA dupok size=8
0x0000 01 00 00 00 00 00 00 00 …
type:.namedata.
[1]interface {}- SRODATA dupok size=18
0x0000 00 10 2a 5b 31 5d 69 6e 74 65 72 66 61 63 65 20 …
[1]interface
0x0010 7b 7d {}
type:
[1]interface {} SRODATA dupok size=56
0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 …
0x0010 a8 0e 57 36 08 08 08 36 00 00 00 00 00 00 00 00 …W6…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 …
rel 24+8 t=1 runtime.memequal64·f+0
rel 32+8 t=1 runtime.gcbits.0100000000000000+0
rel 40+4 t=5 type:.namedata.
[1]interface {}-+0
rel 48+8 t=1 type:[1]interface {}+0
runtime.gcbits.0200000000000000 SRODATA dupok size=8
0x0000 02 00 00 00 00 00 00 00 …
type:[1]interface {} SRODATA dupok size=72
0x0000 10 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 …
0x0010 6e 20 6a 3d 02 08 08 11 00 00 00 00 00 00 00 00 n j=…
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 …
rel 24+8 t=1 runtime.nilinterequal·f+0
rel 32+8 t=1 runtime.gcbits.0200000000000000+0
rel 40+4 t=5 type:.namedata.
[1]interface {}-+0
rel 44+4 t=-32763 type:
[1]interface {}+0
rel 48+8 t=1 type:interface {}+0
rel 56+8 t=1 type:[]interface {}+0
type:.importpath.fmt. SRODATA dupok size=5
0x0000 00 03 66 6d 74 …fmt
gclocals·J5F+7Qw7O7ve2QcWC7DpeQ== SRODATA dupok size=8
0x0000 02 00 00 00 00 00 00 00 …
gclocals·5aa34RaZcmo0NkRpBHp2fg== SRODATA dupok size=10
0x0000 02 00 00 00 02 00 00 00 00 02 …
main.main.stkobj SRODATA static size=24
0x0000 01 00 00 00 00 00 00 00 f0 ff ff ff 10 00 00 00 …
0x0010 10 00 00 00 00 00 00 00 …
rel 20+4 t=5 runtime.gcbits.0200000000000000+0

写回答

1回答

Moody

2023-11-20

这个代码被优化了,第六行编译时被删掉了,第七行被优化成了:

fmt.println("123")


0
1
煎鱼侠
非常感谢!
2023-11-26
共1条回复

深入Go底层原理,重写Redis中间件实战

深入Go语言原理+Go重写Redis

474 学习 · 172 问题

查看课程