go string的内部实现

简介: # go string 内部实现 这个string的探索 来来个例子 ``` func boo(a int, b int)(int, string){ return a + b, "abcd" } ``` ``` 81079 000000000044dfa0 : 81080 44dfa0:>------48 c7 44 24 18 00 00 >--movq $0x0

go string 内部实现

这个string的探索
来来个例子

func boo(a int, b int)(int, string){
    return a + b, "abcd"
}
81079 000000000044dfa0 <main.boo>:
81080   44dfa0:>------48 c7 44 24 18 00 00 >--movq   $0x0,0x18(%rsp)
81081   44dfa7:>------00 00- 
81082   44dfa9:>------0f 57 c0             >--xorps  %xmm0,%xmm0
81083   44dfac:>------0f 11 44 24 20       >--movups %xmm0,0x20(%rsp)
81084   44dfb1:>------48 8b 44 24 08       >--mov    0x8(%rsp),%rax
81085   44dfb6:>------48 03 44 24 10       >--add    0x10(%rsp),%rax
81086   44dfbb:>------48 89 44 24 18       >--mov    %rax,0x18(%rsp)
81087   44dfc0:>------48 8d 05 d4 eb 01 00 >--lea    0x1ebd4(%rip),%rax        # 46cb9b <go.string.*+0xbb>
81088   44dfc7:>------48 89 44 24 20       >--mov    %rax,0x20(%rsp)
81089   44dfcc:>------48 c7 44 24 28 04 00 >--movq   $0x4,0x28(%rsp)
81090   44dfd3:>------00 00- 
81091   44dfd5:>------c3                   >--retq---

其中

81087   44dfc0:>------48 8d 05 d4 eb 01 00 >--lea    0x1ebd4(%rip),%rax        # 46cb9b <go.string.*+0xbb>
81088   44dfc7:>------48 89 44 24 20       >--mov    %rax,0x20(%rsp)
81089   44dfcc:>------48 c7 44 24 28 04 00 >--movq   $0x4,0x28(%rsp)
81090   44dfd3:>------00 00- 
81091   44dfd5:>------c3                   >--retq---

lea 0x1ebd4(%rip),%rax得到char*, mov %rax,0x20(%rsp)复制给返回值, movq $0x4,0x28(%rsp)把长度也填进去,

其实可以看到string就是c里面的char* 和len的组合

目录
相关文章
|
4月前
|
Go
Go string bytes、strings、strconv和unicode包相关方法
Go string bytes、strings、strconv和unicode包相关方法
43 0
|
10月前
|
Go
go string to int 字符串与整数型的互换
go string to int 字符串与整数型的互换
50 0
|
1月前
|
Go
Go - 学习 grpc.Dial(target string, opts …DialOption) 的写法
Go - 学习 grpc.Dial(target string, opts …DialOption) 的写法
44 12
|
1月前
|
存储 安全 Go
Go 面试题:string 是线程安全的吗?
Go 面试题:string 是线程安全的吗?
|
4月前
|
Go C语言
Go新手速成-string
Go新手速成-string
|
10月前
|
存储 监控 搜索推荐
GO 中 string 的实现原理
GO 中 string 的实现原理
|
11月前
|
Go 开发者 索引
100天精通Golang(基础入门篇)——第13天:深入解析Go语言中的字符串(string)及常用函数应用
100天精通Golang(基础入门篇)——第13天:深入解析Go语言中的字符串(string)及常用函数应用
83 0
|
11月前
|
存储 Go C语言
Go学习笔记-String底层实现原理
Go学习笔记-String底层实现原理
64 0
|
11月前
|
存储 安全 编译器
Go语言源码剖析-String和unsafe包
Go语言源码剖析-String和unsafe包
62 0
|
Go
Go处理中文 string 转为 rune 数组
Go处理中文 string 转为 rune 数组
112 0