Closed
Description
package main
import "fmt"
type T int
func (T) String() string { return "T" }
type S struct {
t T
}
func main() {
var t T
fmt.Printf("%s\n", t)
var s S
fmt.Printf("%s\n", s)
}
$ go tool vet x.go
$ go run x.go
T
{%!s(main.T=0)}
vet should know that S isn't a fmt.Stringer. The printfunc's "satisfies interface" check needs improvements. Related is #17057.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
valyala commentedon Nov 4, 2016
I'll look into this and the #17057 on the weekend.
valyala commentedon Nov 4, 2016
FYI, it looks like the cl 28959 breaks printf check on
errror
interfaces:valyala commentedon Nov 4, 2016
I think it would be better to fix these issues in go 1.8.
josharian commentedon Nov 4, 2016
CL 28959 doesn't change cmd/vet at all. It only changes cmd/vet/all, which is a script that runs cmd/vet over the standard library.
If the changes are fairly safe and we can get them in soon, I think that's fine.
josharian commentedon Nov 4, 2016
Btw, this is intentional. See #16314.
valyala commentedon Jun 2, 2017
This looks like a bug in package
fmt
- it must properly print all the struct fields if they implementfmt.Stringer
according to the rule:In this case struct
S
contains only a single field of typeT
, which implements Stringer. Sofmt.Printf
must print contents of the struct -{T}
instead of printing{%!s(main.T=0)}
.fmt.Printf
works as expected for slices, arrays and maps withfmt.Stringer
values, but doesn't work for structs:valyala commentedon Jun 2, 2017
cc'ing @ALTree , @martisch and @robpike
martisch commentedon Jun 2, 2017
the field t is not exported. If t is changed to be exported fmt will print {T}. fmt is in another package and does not have access to call the String method of t if it is not exported.
see also #18281 #17409 #16698
gopherbot commentedon Jun 5, 2017
CL https://golang.org/cl/44831 mentions this issue.
mvdan commentedon Jun 16, 2017
CL was marked as R=go1.10, so moving the issue milestone too.
gopherbot commentedon Jan 29, 2018
Change https://golang.org/cl/90516 mentions this issue:
cmd/vet: unexported interface fields on %s are ok
cmd/vet: unexported interface{} fields on %s are ok