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