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
I was expecting an error because there is no documentation regarding %p with slices in https://golang.org/pkg/fmt/ but it works fine.
I was surprised by the output as the printed address changed when the underlying array of the slice was modified. This is the expected behaviour because fmt is using the reflect package's Pointer() method on the slice, which will return the address of the first element of the slice. See
The valid reflect types for the verb %p are listed here: https://github.com/golang/go/blob/master/src/fmt/print.go#L495
If CL with documentation for %p on slices is created it could be expanded to cover all possible types that are not already documented.
mewmew
added a commit
to mewpull/go
that referenced
this issue
Mar 29, 2018
PR #24600 seeks to address this issue, by documenting that %p on a slice prints the address of its first element (rather than the address of the slice descriptor).
I was surprised by the following program's output:
Example output:
I was expecting an error because there is no documentation regarding
%p
with slices in https://golang.org/pkg/fmt/ but it works fine.I was surprised by the output as the printed address changed when the underlying array of the slice was modified. This is the expected behaviour because fmt is using the reflect package's Pointer() method on the slice, which will return the address of the first element of the slice. See
go/src/reflect/value.go
Lines 1267 to 1269 in 38c561c
There should be some documentation regarding this behaviour because it is unexpected imo.
The text was updated successfully, but these errors were encountered: