Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: add flag "@" to format a verb "v" value with pretty style(indented-multi-lines) string #2

Closed
vipally opened this issue Dec 22, 2017 · 0 comments
Assignees
Labels

Comments

@vipally
Copy link
Owner

vipally commented Dec 22, 2017

refer golang#23026

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.
"%#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}}

I have made a patch to use "%@#v" "%@+v" to format a value with indented-multi-lines style string to extends "%#v" "%+v".
I expect this feature can be accepted by go team, and it will be my pleasure to contribute this patch to go repository.

Branch
Example

here is the expected example

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
	//     ]
	// }
	//
}

Branch
Example

@vipally vipally changed the title fmt: use "@" verb to format a value with pretty(indented-multi-lines) style string fmt: use "@" verb to format a value with pretty style(indented-multi-lines) string Dec 22, 2017
@vipally vipally self-assigned this Dec 22, 2017
@vipally vipally changed the title fmt: use "@" verb to format a value with pretty style(indented-multi-lines) string fmt: use flag "@" to format a value with pretty style(indented-multi-lines) string Dec 23, 2017
@vipally vipally changed the title fmt: use flag "@" to format a value with pretty style(indented-multi-lines) string fmt: add flag "@" to format a verb ‘v’ value with pretty style(indented-multi-lines) string Dec 23, 2017
@vipally vipally changed the title fmt: add flag "@" to format a verb ‘v’ value with pretty style(indented-multi-lines) string fmt: add flag "@" to format a verb "v" value with pretty style(indented-multi-lines) string Dec 23, 2017
vipally added a commit that referenced this issue Dec 23, 2017
…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".
@vipally vipally closed this as completed Dec 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant