1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This is a package for testing comment placement by go/printer.
17 // Alignment of comments in declarations>
47 // The SZ struct; it is empty.
50 // The S0 struct; no field is exported.
53 x, y, z int // 3 unexported fields
56 // The S1 struct; some fields are not exported.
59 A, B, C float // 3 exported fields
60 D, b, c int // 2 unexported fields
63 // The S2 struct; all fields are exported.
66 A, B, C float // 3 exported fields
69 // The IZ interface; it is empty.
72 // The I0 interface; no method is exported.
74 f(x int) int // unexported method
77 // The I1 interface; some methods are not exported.
80 F(x float) float // exported methods
81 g(x int) int // unexported method
84 // The I2 interface; all methods are exported.
87 F(x float) float // exported method
88 G(x float) float // exported method
91 // The S3 struct; all comments except for the last one must appear in the export.
93 // lead comment for F1
94 F1 int // line comment for F1
95 // lead comment for F2
96 F2 int // line comment for F2
97 f3 int // f3 is not exported
100 // This comment group should be separated
101 // with a newline from the next comment
104 // This comment should NOT be associated with the next declaration.
109 // This comment SHOULD be associated with f0.
111 const pi = 3.14 // pi
112 var s1 struct{} /* an empty struct */ /* foo */
113 // a struct constructor
114 // --------------------
115 var s2 struct{} = struct{}{}
120 // This comment should be associated with f1, with one blank line before the comment.
132 // this comment should be properly indented
136 if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
137 return -x // this statement should be properly indented
139 if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */
140 return -x // this statement should be properly indented
145 func typeswitch(x interface{}) {
146 switch v := x.(type) {
147 case bool, int, float:
155 switch v0, ok := x.(int); v := x.(type) {
158 switch v0, ok := x.(int); x.(type) {
159 case byte: // this comment should be on the same line as the keyword
160 // this comment should be normally indented
162 case bool, int, float:
163 // this comment should be indented
166 // this comment should be indented
168 // this comment should not be indented
172 /* freestanding comment
179 /* freestanding comment
186 /* freestanding comment
192 /* freestanding comment
199 /* freestanding comment
206 /* freestanding comment
258 /* freestanding comment
264 /* freestanding comment
270 /* freestanding comment
275 /* freestanding comment
281 /* freestanding comment
287 /* freestanding comment
348 /* and another line of
354 /* and another line of
360 /* and another line of
370 blank line in middle:
372 with no leading spaces on blank line.
382 blank line in middle:
384 with no leading spaces on blank line.
401 blank line in middle:
403 with no leading spaces on blank line.
407 // Some interesting interspersed comments
408 func _( /* this */ x /* is */ /* an */ int) {
411 func _( /* no params */ ) {}
417 func ( /* comment1 */ T /* comment2 */ ) _() {}
419 func _() { /* one-line functions with comments are formatted as multi-line functions */
424 /* closing curly brace should be on new line */
428 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */ }
431 // Comments immediately adjacent to punctuation (for which the go/printer
432 // may only have estimated position information) must remain after the punctuation.
435 1, // comment after comma
436 2, /* comment after comma */
437 3, // comment after comma
440 1, // comment after comma
441 2, /* comment after comma */
442 3, // comment after comma
445 /* comment before literal */ 1,
446 2, /* comment before comma - ok to move after comma */
447 3, /* comment before comma - ok to move after comma */
450 for i = 0; // comment after semicolon
451 i < 9; /* comment after semicolon */
452 i++ { // comment after opening curly brace
455 // TODO(gri) the last comment in this example should be aligned */
456 for i = 0; // comment after semicolon
457 i < 9; /* comment before semicolon - ok to move after semicolon */
458 i++ /* comment before opening curly brace */ {
462 // Line comments with tabs
464 var finput *bufio.Reader // input file
465 var stderr *bufio.Writer
466 var ftable *bufio.Writer // y.go file
467 var foutput *bufio.Writer // y.output file
469 var oflag string // -o [y.go] - y.go file
470 var vflag string // -v [y.output] - y.output file
471 var lflag bool // -l - disable line directives
474 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */