You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fmt.Print{f, ln} function generates a stackoverflow when the format flag is %v to output the value of a type that implements the Stringer interface. This is true when the implementation of the String() method also uses fmt with flag %v or %s flag to output the string as shown in the example below.
1)What version of Go are you using (go version)? go version go1.4.2 linux/amd64
2) What operating system and processor architecture are you using?
Ubuntu 14 trusty
Linux Kernel 3.14
3)What did you do ?
The following simple program.
package main
import (
"fmt"
)
type mytype string
func (m mytype) String() string {
return fmt.Sprintf("%s", m)
}
func main() {
s := mytype("hello")
fmt.Printf("%v", s)
}
What did you expect to see?
Expects the value of s to be printed.
The text was updated successfully, but these errors were encountered:
bradfitz
changed the title
GoStringer value cause Stack Overflow when %V or %S used in implementation
x/tools/cmd/stringer: value cause stack overflow with %V or %S
Aug 23, 2015
minux
changed the title
x/tools/cmd/stringer: value cause stack overflow with %V or %S
fmt: value implementing Stringer (GoString) cause infinite recursion with %s (%v)
Aug 24, 2015
This is working as intended. You need to change the String method to
convert m into a real string before passing it to fmt.Sprintf, otherwise
the fmt.Sprintf will just call the String method again, which will lead to
infinite recursion.
The fmt.Print{f, ln} function generates a stackoverflow when the format flag is
%v
to output the value of a type that implements the Stringer interface. This is true when the implementation of the String() method also usesfmt
with flag%v
or%s
flag to output the string as shown in the example below.1)What version of Go are you using (go version)?
go version go1.4.2 linux/amd64
2) What operating system and processor architecture are you using?
Ubuntu 14 trusty
Linux Kernel 3.14
3)What did you do ?
The following simple program.
Expects the value of
s
to be printed.5) What did you see instead?
The text was updated successfully, but these errors were encountered: