fmt: stack overflow
2016-05-15 18:53
232 查看
package main import ( "fmt" ) type Str string func (s Str) String() string { return fmt.Sprintf("Str: %s", s) } func main() { var s Str = "hi" fmt.Println(s) }
RunCode:
runtime: goroutine stack exceeds 250000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x4e33b0, 0xe)
D:/go/src/runtime/panic.go:530 +0x7f
runtime.newstack()
D:/go/src/runtime/stack.go:940 +0x9a7
runtime.morestack()
D:/go/src/runtime/asm_386.s:382 +0x6f
goroutine 1 [stack growth]:
runtime.heapBitsSetType(0x1270c200, 0x8, 0x8, 0x4c1860)
D:/go/src/runtime/mbitmap.go:679 fp=0x18b10a7c sp=0x18b10a78
.....
原因:
You are implementing
Str.Stringin
terms of itself.
return fmt.Sprintf("Str: %s", s)will
call s.String(), resulting in infinite recursion. Convert s to string first.
This
is working as intended, you are using the %s verb to call Str's String method, which uses fmt.Sprint to call Str's String method, and so on.
修改后代码:
package main import ( "fmt" ) type Str string func (s Str) String() string { return fmt.Sprintf("Str: %s", string(s)) } func main() { var s Str = "hi" fmt.Println(s) }
相关文章推荐
- 多余元素删除之建表算法
- 第十一周学习进度条
- iOS开发系列--UITableView全面解析
- 第十一周进度条
- git常用命令清单
- Tomcat安装及配置
- 解释器模式
- java中接口类似c++中纯虚函数
- linux安全---利用DenyHosts防止ssh暴力破解
- Java字节码操纵框架ASM快速入门
- 同步-异步-阻塞-非阻塞
- HDU 5682 zxa and leaf 二分 树形dp
- 即使已毕业多年,这些道理依然受用
- Shell脚本学习指南(四)——文本处理工具
- vs2013下oracle proc配置
- 各种排序算法的比较——一步一步算法篇
- CentOS 7 java服务器搭建
- 学习进度条(第十一周)
- 基本 vi 命令
- 2016-5-15 linux基础学习——case语句