dongshen2903 2017-10-15 19:13
浏览 38
已采纳

具有int类型的Stringer接口

I'm just learning the golang language via Tour of Go and I reached the Stringer part. (https://tour.golang.org/methods/17)

I try to understand how it works so I tried it int tpye. It did not work.

The code:

package main

import (
    "fmt"
)

type I int

func (i I) String() string {
    return fmt.Sprintf("%v", i)
}

func main() {   
    i := I(10)
    fmt.Println(i)
}

When I run the example I got an error and I do not quite understand what is the probem.

My theory is this with String() method: When I run an printer code, the program looks for stringer method and if It is exists runs it. It works like this?

Can anyone explain this to me how does this exactly work?

ERROR (in the Tour of Go):

runtime: goroutine stack exceeds 250000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0xff4a2, 0xe)
    /usr/local/go/src/runtime/panic.go:605 +0x100
runtime.newstack(0x0, 0x0)
    /usr/local/go/src/runtime/stack.go:1050 +0x960
runtime.morestack()
    /usr/local/go/src/runtime/asm_amd64p32.s:378 +0xa0

goroutine 1 [running]:
runtime.mallocgc(0x4, 0xef280, 0x18500300, 0x0, 0x0, 0x0)
    /usr/local/go/src/runtime/malloc.go:581 +0xb60 fp=0x18500360 sp=0x18500358 pc=0x31440
runtime.convT2E32(0xef280, 0x185003a4, 0x83cc0, 0x10)
    /usr/local/go/src/runtime/iface.go:247 +0x80 fp=0x18500380 sp=0x18500360 pc=0x2e000
main.I.String(0xa, 0x185003d0, 0x185003d4, 0x0)
    /tmp/sandbox580986616/main.go:10 +0x40 fp=0x185003b8 sp=0x18500380 pc=0xd4f60
main.(*I).String(0x1391de68, 0x11d050, 0x13972a80, 0xef280)
    <autogenerated>:1 +0x40 fp=0x185003d0 sp=0x185003b8 pc=0xd51c0
fmt.(*pp).handleMethods(0x13972a80, 0x76, 0x1, 0x0)
    /usr/local/go/src/fmt/print.go:596 +0x3a0 fp=0x18500420 sp=0x185003d0 pc=0xcdf60
fmt.(*pp).printArg(0x13972a80, 0xef280, 0x1391de68, 0x76)
    /usr/local/go/src/fmt/print.go:679 +0x1a0 fp=0x18500468 sp=0x18500420 pc=0xce540
fmt.(*pp).doPrintf(0x13972a80, 0xfe0ce, 0x2, 0x18500560, 0x1, 0x1)
    /usr/local/go/src/fmt/print.go:996 +0x180 fp=0x18500508 sp=0x18500468 pc=0xd32c0
fmt.Sprintf(0xfe0ce, 0x2, 0x18500560, 0x1, 0x1, 0x0, 0x0, 0x0)
    /usr/local/go/src/fmt/print.go:196 +0x60 fp=0x18500538 sp=0x18500508 pc=0xca160
main.I.String(0xa, 0x18500588, 0x1850058c, 0x0).......
  • 写回答

2条回答 默认 最新

  • douhuanqiao5290 2017-10-15 19:25
    关注

    Your problem is simple. You pass a value of type I to fmt.Println, wich calls Is String method, which calls fmt.Sprintf with a value of type I, which calls Is String method...

    Just change the body of Is String method to: return fmt.Sprintf("%v", int(i)) and you should be good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证