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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法