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 the next declaration.
111 const pi = 3.14 // pi
112 var s1 struct{} /* an empty struct */ /* foo */
113 // a struct constructor
114 // --------------------
115 var s2 struct{} = struct{}{}
131 // this comment should be properly indented
135 if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
136 return -x // this statement should be properly indented
138 if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */
139 return -x // this statement should be properly indented
144 func typeswitch(x interface{}) {
145 switch v := x.(type) {
146 case bool, int, float:
154 switch v0, ok := x.(int); v := x.(type) {
157 switch v0, ok := x.(int); x.(type) {
158 case byte: // this comment should be on the same line as the keyword
159 // this comment should be normally indented
161 case bool, int, float:
162 // this comment should be indented
165 // this comment should be indented
167 // this comment should not be indented
171 /* freestanding comment
178 /* freestanding comment
185 /* freestanding comment
191 /* freestanding comment
198 /* freestanding comment
205 /* freestanding comment
257 /* freestanding comment
263 /* freestanding comment
269 /* freestanding comment
274 /* freestanding comment
280 /* freestanding comment
286 /* freestanding comment
347 /* and another line of
353 /* and another line of
359 /* and another line of
369 blank line in middle:
371 with no leading spaces on blank line.
381 blank line in middle:
383 with no leading spaces on blank line.
400 blank line in middle:
402 with no leading spaces on blank line.
406 // Some interesting interspersed comments
407 func _( /* this */ x /* is */ /* an */ int) {
410 func _( /* no params */ ) {}
416 func ( /* comment1 */ T /* comment2 */ ) _() {}
418 func _() { /* one-line functions with comments are formatted as multi-line functions */
423 /* closing curly brace should be on new line */
427 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */ }
430 // Comments immediately adjacent to punctuation (for which the go/printer
431 // may only have estimated position information) must remain after the punctuation.
434 1, // comment after comma
435 2, /* comment after comma */
436 3, // comment after comma
439 1, // comment after comma
440 2, /* comment after comma */
441 3, // comment after comma
444 /* comment before literal */ 1,
445 2, /* comment before comma - ok to move after comma */
446 3, /* comment before comma - ok to move after comma */
449 for i = 0; // comment after semicolon
450 i < 9; /* comment after semicolon */
451 i++ { // comment after opening curly brace
454 // TODO(gri) the last comment in this example should be aligned */
455 for i = 0; // comment after semicolon
456 i < 9; /* comment before semicolon - ok to move after semicolon */
457 i++ /* comment before opening curly brace */ {
461 // Line comments with tabs
463 var finput *bufio.Reader // input file
464 var stderr *bufio.Writer
465 var ftable *bufio.Writer // y.go file
466 var foutput *bufio.Writer // y.output file
468 var oflag string // -o [y.go] - y.go file
469 var vflag string // -v [y.output] - y.output file
470 var lflag bool // -l - disable line directives
473 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */