Skip to content

Commit

Permalink
fmt: add flag "@" to format a verb "v" value with pretty style(indent…
Browse files Browse the repository at this point in the history
…ed-multi-lines) string (#7)

fmt: add flag "@" to format a verb "v" value with pretty style(indented-multi-lines) string #2
Current custom go data printing style "%#v" show all data in a single line, which is not easy to read, especially in a debugging process.
I have made a patch to use "%@#v" "%@+v" to format a value with pretty style(indented-multi-lines) string to extends "%#v" "%+v".
  • Loading branch information
vipally authored Dec 23, 2017
1 parent 5bbaec2 commit 55a970c
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ally master branch ![Version][version-img] [![Build status][travis-img]][travis-url]
changlist from Official:
-
- [#2: fmt: add flag "@" to format a verb "v" value with pretty style(indented-multi-lines) string](https://github.com/vipally/go/issues/2)
- [#1: runtime, time: add API BuildTimestamp to report an application's build time](https://github.com/vipally/go/issues/1)
- [func runtime.BuildTimestamp() int64](https://github.com/vipally/go/blob/ally_master/src/runtime/time.go#L21)
- [func time.BuildTime() time.Time](https://github.com/vipally/go/blob/ally_master/src/time/time.go#L1517)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/vet/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (f *File) checkPrintf(call *ast.CallExpr, name string) {
func (s *formatState) parseFlags() {
for s.nbytes < len(s.format) {
switch c := s.format[s.nbytes]; c {
case '#', '0', '+', '-', ' ':
case '#', '0', '+', '-', ' ', '@':
s.flags = append(s.flags, c)
s.nbytes++
default:
Expand Down
15 changes: 10 additions & 5 deletions src/fmt/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
The verbs:
General:
%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value
%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
%@v the value in a default pretty format
%#v a Go-syntax representation of the value
%@#v pretty style of Go-syntax representation of the value(an indented-multi-lines string)
%+v when printing structs, the plus flag (%+v) adds field names.
%@+v pretty style of struct-syntax representation of the value(an indented-multi-lines string)
%T a Go-syntax representation of the type of the value
%% a literal percent sign; consumes no value
Boolean:
%t the word true or false
Expand Down Expand Up @@ -117,6 +121,7 @@
write e.g. U+0078 'x' if the character is printable for %U (%#U).
' ' (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x, % X)
@ pretty style for verb %v, which will printing as an indented-multi-lines string.
0 pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
Expand Down
137 changes: 137 additions & 0 deletions src/fmt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,140 @@ func ExampleStringer() {
fmt.Println(a)
// Output: Gopher (2)
}

func ExamplePrintf_flagV() {
type X struct {
A int
B string
}
type Y struct {
D X
E []int
F [2]string
}
type Z struct {
G Y
H string
I []string
J map[string]int
}
var z = Z{
G: Y{
D: X{
A: 123,
B: `"b" = 1`,
},
E: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
F: [2]string{
`aaa`,
`bbb`,
},
},
H: `zzz`,
I: []string{
`c:\x\y\z`,
`d:\a\b\c`,
},
J: map[string]int{
`abc`: 456,
},
}
fmt.Printf("-------\n\"%%v\":\n%v\n", z)
fmt.Printf("-------\n\"%%@v\":\n%@v\n", z)
fmt.Printf("-------\n\"%%#v\":\n%#v\n", z)
fmt.Printf("-------\n\"%%@#v\":\n%@#v\n", z)
fmt.Printf("-------\n\"%%+v\":\n%+v\n", z)
fmt.Printf("-------\n\"%%@+v\":\n%@+v\n", z)

// Output:
// -------
// "%v":
// {{{123 "b" = 1} [1 2 3 4 5 6 7 8 9 10 11 12] [aaa bbb]} zzz [c:\x\y\z d:\a\b\c] map[abc:456]}
// -------
// "%@v":
// {
// {
// {
// 123
// "b" = 1
// }
// [
// 1 2 3 4 5 6 7 8 9 10
// 11 12
// ]
// [
// aaa
// bbb
// ]
// }
// zzz
// [
// c:\x\y\z
// d:\a\b\c
// ]
// map[
// abc: 456
// ]
// }
//
// -------
// "%#v":
// fmt_test.Z{G:fmt_test.Y{D:fmt_test.X{A:123, B:"\"b\" = 1"}, E:[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, F:[2]string{"aaa", "bbb"}}, H:"zzz", I:[]string{"c:\\x\\y\\z", "d:\\a\\b\\c"}, J:map[string]int{"abc":456}}
// -------
// "%@#v":
// fmt_test.Z{
// G: fmt_test.Y{
// D: fmt_test.X{
// A: 123,
// B: `"b" = 1`,
// },
// E: []int{
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
// 11, 12,
// },
// F: [2]string{
// `aaa`,
// `bbb`,
// },
// },
// H: `zzz`,
// I: []string{
// `c:\x\y\z`,
// `d:\a\b\c`,
// },
// J: map[string]int{
// `abc`: 456,
// },
// }
//
// -------
// "%+v":
// {G:{D:{A:123 B:"b" = 1} E:[1 2 3 4 5 6 7 8 9 10 11 12] F:[aaa bbb]} H:zzz I:[c:\x\y\z d:\a\b\c] J:map[abc:456]}
// -------
// "%@+v":
// {
// G: {
// D: {
// A: 123
// B: "b" = 1
// }
// E: [
// 1 2 3 4 5 6 7 8 9 10
// 11 12
// ]
// F: [
// aaa
// bbb
// ]
// }
// H: zzz
// I: [
// c:\x\y\z
// d:\a\b\c
// ]
// J: map[
// abc: 456
// ]
// }
//
}
Loading

0 comments on commit 55a970c

Please sign in to comment.