github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/go/printer/testdata/declarations.golden (about) 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. 4 5 package imports 6 7 import "io" 8 9 import ( 10 _ "io" 11 ) 12 13 import _ "io" 14 15 import ( 16 "io" 17 "io" 18 "io" 19 ) 20 21 import ( 22 "io" 23 aLongRename "io" 24 25 b "io" 26 ) 27 28 import ( 29 "unrenamed" 30 renamed "renameMe" 31 . "io" 32 _ "io" 33 "io" 34 . "os" 35 ) 36 37 // no newlines between consecutive single imports, but 38 // respect extra line breaks in the source (at most one empty line) 39 import _ "io" 40 import _ "io" 41 import _ "io" 42 43 import _ "os" 44 import _ "os" 45 import _ "os" 46 47 import _ "fmt" 48 import _ "fmt" 49 import _ "fmt" 50 51 import "foo" // a comment 52 import "bar" // a comment 53 54 import ( 55 _ "foo" 56 // a comment 57 "bar" 58 "foo" // a comment 59 "bar" // a comment 60 ) 61 62 // comments + renames 63 import ( 64 "unrenamed" // a comment 65 renamed "renameMe" 66 . "io" /* a comment */ 67 _ "io/ioutil" // a comment 68 "io" // testing alignment 69 . "os" 70 // a comment 71 ) 72 73 // a case that caused problems in the past (comment placement) 74 import ( 75 . "fmt" 76 "io" 77 "malloc" // for the malloc count test only 78 "math" 79 "strings" 80 "testing" 81 ) 82 83 // more import examples 84 import ( 85 "xxx" 86 "much_longer_name" // comment 87 "short_name" // comment 88 ) 89 90 import ( 91 _ "xxx" 92 "much_longer_name" // comment 93 ) 94 95 import ( 96 mymath "math" 97 "/foo/bar/long_package_path" // a comment 98 ) 99 100 import ( 101 "package_a" // comment 102 "package_b" 103 my_better_c "package_c" // comment 104 "package_d" // comment 105 my_e "package_e" // comment 106 107 "package_a" // comment 108 "package_bb" 109 "package_ccc" // comment 110 "package_dddd" // comment 111 ) 112 113 // at least one empty line between declarations of different kind 114 import _ "io" 115 116 var _ int 117 118 // at least one empty line between declarations of the same kind 119 // if there is associated documentation (was issue 2570) 120 type T1 struct{} 121 122 // T2 comment 123 type T2 struct { 124 } // should be a two-line struct 125 126 // T3 comment 127 type T2 struct { 128 } // should be a two-line struct 129 130 // printing of constant literals 131 const ( 132 _ = "foobar" 133 _ = "a۰۱۸" 134 _ = "foo६४" 135 _ = "bar9876" 136 _ = 0 137 _ = 1 138 _ = 123456789012345678890 139 _ = 01234567 140 _ = 0xcafebabe 141 _ = 0. 142 _ = .0 143 _ = 3.14159265 144 _ = 1e0 145 _ = 1e+100 146 _ = 1e-100 147 _ = 2.71828e-1000 148 _ = 0i 149 _ = 1i 150 _ = 012345678901234567889i 151 _ = 123456789012345678890i 152 _ = 0.i 153 _ = .0i 154 _ = 3.14159265i 155 _ = 1e0i 156 _ = 1e+100i 157 _ = 1e-100i 158 _ = 2.71828e-1000i 159 _ = 'a' 160 _ = '\000' 161 _ = '\xFF' 162 _ = '\uff16' 163 _ = '\U0000ff16' 164 _ = `foobar` 165 _ = `foo 166 --- 167 --- 168 bar` 169 ) 170 171 func _() { 172 type _ int 173 type _ *int 174 type _ []int 175 type _ map[string]int 176 type _ chan int 177 type _ func() int 178 179 var _ int 180 var _ *int 181 var _ []int 182 var _ map[string]int 183 var _ chan int 184 var _ func() int 185 186 type _ struct{} 187 type _ *struct{} 188 type _ []struct{} 189 type _ map[string]struct{} 190 type _ chan struct{} 191 type _ func() struct{} 192 193 type _ interface{} 194 type _ *interface{} 195 type _ []interface{} 196 type _ map[string]interface{} 197 type _ chan interface{} 198 type _ func() interface{} 199 200 var _ struct{} 201 var _ *struct{} 202 var _ []struct{} 203 var _ map[string]struct{} 204 var _ chan struct{} 205 var _ func() struct{} 206 207 var _ interface{} 208 var _ *interface{} 209 var _ []interface{} 210 var _ map[string]interface{} 211 var _ chan interface{} 212 var _ func() interface{} 213 } 214 215 // don't lose blank lines in grouped declarations 216 const ( 217 _ int = 0 218 _ float = 1 219 220 _ string = "foo" 221 222 _ = iota 223 _ 224 225 // a comment 226 _ 227 228 _ 229 ) 230 231 type ( 232 _ int 233 _ struct{} 234 235 _ interface{} 236 237 // a comment 238 _ map[string]int 239 ) 240 241 var ( 242 _ int = 0 243 _ float = 1 244 245 _ string = "foo" 246 247 _ bool 248 249 // a comment 250 _ bool 251 ) 252 253 // don't lose blank lines in this struct 254 type _ struct { 255 String struct { 256 Str, Len int 257 } 258 Slice struct { 259 Array, Len, Cap int 260 } 261 Eface struct { 262 Typ, Ptr int 263 } 264 265 UncommonType struct { 266 Name, PkgPath int 267 } 268 CommonType struct { 269 Size, Hash, Alg, Align, FieldAlign, String, UncommonType int 270 } 271 Type struct { 272 Typ, Ptr int 273 } 274 StructField struct { 275 Name, PkgPath, Typ, Tag, Offset int 276 } 277 StructType struct { 278 Fields int 279 } 280 PtrType struct { 281 Elem int 282 } 283 SliceType struct { 284 Elem int 285 } 286 ArrayType struct { 287 Elem, Len int 288 } 289 290 Stktop struct { 291 Stackguard, Stackbase, Gobuf int 292 } 293 Gobuf struct { 294 Sp, Pc, G int 295 } 296 G struct { 297 Stackbase, Sched, Status, Alllink int 298 } 299 } 300 301 // no blank lines in empty structs and interfaces, but leave 1- or 2-line layout alone 302 type _ struct{} 303 type _ struct { 304 } 305 306 type _ interface{} 307 type _ interface { 308 } 309 310 // no tabs for single or ungrouped decls 311 func _() { 312 const xxxxxx = 0 313 type x int 314 var xxx int 315 var yyyy float = 3.14 316 var zzzzz = "bar" 317 318 const ( 319 xxxxxx = 0 320 ) 321 type ( 322 x int 323 ) 324 var ( 325 xxx int 326 ) 327 var ( 328 yyyy float = 3.14 329 ) 330 var ( 331 zzzzz = "bar" 332 ) 333 } 334 335 // tabs for multiple or grouped decls 336 func _() { 337 // no entry has a type 338 const ( 339 zzzzzz = 1 340 z = 2 341 zzz = 3 342 ) 343 // some entries have a type 344 const ( 345 xxxxxx = 1 346 x = 2 347 xxx = 3 348 yyyyyyyy float = iota 349 yyyy = "bar" 350 yyy 351 yy = 2 352 ) 353 } 354 355 func _() { 356 // no entry has a type 357 var ( 358 zzzzzz = 1 359 z = 2 360 zzz = 3 361 ) 362 // no entry has a value 363 var ( 364 _ int 365 _ float 366 _ string 367 368 _ int // comment 369 _ float // comment 370 _ string // comment 371 ) 372 // some entries have a type 373 var ( 374 xxxxxx int 375 x float 376 xxx string 377 yyyyyyyy int = 1234 378 y float = 3.14 379 yyyy = "bar" 380 yyy string = "foo" 381 ) 382 // mixed entries - all comments should be aligned 383 var ( 384 a, b, c int 385 x = 10 386 d int // comment 387 y = 20 // comment 388 f, ff, fff, ffff int = 0, 1, 2, 3 // comment 389 ) 390 // respect original line breaks 391 var _ = []T{ 392 T{0x20, "Telugu"}, 393 } 394 var _ = []T{ 395 // respect original line breaks 396 T{0x20, "Telugu"}, 397 } 398 } 399 400 func _() { 401 type ( 402 xxxxxx int 403 x float 404 xxx string 405 xxxxx []x 406 xx struct{} 407 xxxxxxx struct { 408 _, _ int 409 _ float 410 } 411 xxxx chan<- string 412 ) 413 } 414 415 // alignment of "=" in consecutive lines (extended example from issue 1414) 416 const ( 417 umax uint = ^uint(0) // maximum value for a uint 418 bpu = 1 << (5 + umax>>63) // bits per uint 419 foo 420 bar = -1 421 ) 422 423 // typical enum 424 const ( 425 a MyType = iota 426 abcd 427 b 428 c 429 def 430 ) 431 432 // excerpt from godoc.go 433 var ( 434 goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory") 435 testDir = flag.String("testdir", "", "Go root subdirectory - for testing only (faster startups)") 436 pkgPath = flag.String("path", "", "additional package directories (colon-separated)") 437 filter = flag.String("filter", "", "filter file containing permitted package directory paths") 438 filterMin = flag.Int("filter_minutes", 0, "filter file update interval in minutes; disabled if <= 0") 439 filterDelay delayTime // actual filter update interval in minutes; usually filterDelay == filterMin, but filterDelay may back off exponentially 440 ) 441 442 // formatting of structs 443 type _ struct{} 444 445 type _ struct { /* this comment should be visible */ 446 } 447 448 type _ struct { 449 // this comment should be visible and properly indented 450 } 451 452 type _ struct { // this comment must not change indentation 453 f int 454 f, ff, fff, ffff int 455 } 456 457 type _ struct { 458 string 459 } 460 461 type _ struct { 462 string // comment 463 } 464 465 type _ struct { 466 string "tag" 467 } 468 469 type _ struct { 470 string "tag" // comment 471 } 472 473 type _ struct { 474 f int 475 } 476 477 type _ struct { 478 f int // comment 479 } 480 481 type _ struct { 482 f int "tag" 483 } 484 485 type _ struct { 486 f int "tag" // comment 487 } 488 489 type _ struct { 490 bool 491 a, b, c int 492 int "tag" 493 ES // comment 494 float "tag" // comment 495 f int // comment 496 f, ff, fff, ffff int // comment 497 g float "tag" 498 h float "tag" // comment 499 } 500 501 type _ struct { 502 a, b, 503 c, d int // this line should be indented 504 u, v, w, x float // this line should be indented 505 p, q, 506 r, s float // this line should be indented 507 } 508 509 // difficult cases 510 type _ struct { 511 bool // comment 512 text []byte // comment 513 } 514 515 // formatting of interfaces 516 type EI interface{} 517 518 type _ interface { 519 EI 520 } 521 522 type _ interface { 523 f() 524 fffff() 525 } 526 527 type _ interface { 528 EI 529 f() 530 fffffg() 531 } 532 533 type _ interface { // this comment must not change indentation 534 EI // here's a comment 535 f() // no blank between identifier and () 536 fffff() // no blank between identifier and () 537 gggggggggggg(x, y, z int) // hurray 538 } 539 540 // formatting of variable declarations 541 func _() { 542 type day struct { 543 n int 544 short, long string 545 } 546 var ( 547 Sunday = day{0, "SUN", "Sunday"} 548 Monday = day{1, "MON", "Monday"} 549 Tuesday = day{2, "TUE", "Tuesday"} 550 Wednesday = day{3, "WED", "Wednesday"} 551 Thursday = day{4, "THU", "Thursday"} 552 Friday = day{5, "FRI", "Friday"} 553 Saturday = day{6, "SAT", "Saturday"} 554 ) 555 } 556 557 // formatting of multi-line variable declarations 558 var a1, b1, c1 int // all on one line 559 560 var a2, b2, 561 c2 int // this line should be indented 562 563 var ( 564 a3, b3, 565 c3, d3 int // this line should be indented 566 a4, b4, c4 int // this line should be indented 567 ) 568 569 // Test case from issue 3304: multi-line declarations must end 570 // a formatting section and not influence indentation of the 571 // next line. 572 var ( 573 minRefreshTimeSec = flag.Int64("min_refresh_time_sec", 604800, 574 "minimum time window between two refreshes for a given user.") 575 x = flag.Int64("refresh_user_rollout_percent", 100, 576 "temporary flag to ramp up the refresh user rpc") 577 aVeryLongVariableName = stats.GetVarInt("refresh-user-count") 578 ) 579 580 func _() { 581 var privateKey2 = &Block{Type: "RSA PRIVATE KEY", 582 Headers: map[string]string{}, 583 Bytes: []uint8{0x30, 0x82, 0x1, 0x3a, 0x2, 0x1, 0x0, 0x2, 584 0x41, 0x0, 0xb2, 0x99, 0xf, 0x49, 0xc4, 0x7d, 0xfa, 0x8c, 585 0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0x3b, 0x6a, 0x13, 586 0x64, 0x2b, 0x23, 0xf2, 0x8b, 0x0, 0x3b, 0xfb, 0x97, 0x79, 587 }, 588 } 589 } 590 591 func _() { 592 var Universe = Scope{ 593 Names: map[string]*Ident{ 594 // basic types 595 "bool": nil, 596 "byte": nil, 597 "int8": nil, 598 "int16": nil, 599 "int32": nil, 600 "int64": nil, 601 "uint8": nil, 602 "uint16": nil, 603 "uint32": nil, 604 "uint64": nil, 605 "float32": nil, 606 "float64": nil, 607 "string": nil, 608 609 // convenience types 610 "int": nil, 611 "uint": nil, 612 "uintptr": nil, 613 "float": nil, 614 615 // constants 616 "false": nil, 617 "true": nil, 618 "iota": nil, 619 "nil": nil, 620 621 // functions 622 "cap": nil, 623 "len": nil, 624 "new": nil, 625 "make": nil, 626 "panic": nil, 627 "panicln": nil, 628 "print": nil, 629 "println": nil, 630 }, 631 } 632 } 633 634 // alignment of map composite entries 635 var _ = map[int]int{ 636 // small key sizes: always align even if size ratios are large 637 a: a, 638 abcdefghabcdefgh: a, 639 ab: a, 640 abc: a, 641 abcdefgabcdefg: a, 642 abcd: a, 643 abcde: a, 644 abcdef: a, 645 646 // mixed key sizes: align when key sizes change within accepted ratio 647 abcdefgh: a, 648 abcdefghabcdefg: a, 649 abcdefghij: a, 650 abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // outlier - do not align with previous line 651 abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // align with previous line 652 653 ab: a, // do not align with previous line 654 abcde: a, // align with previous line 655 } 656 657 // alignment of map composite entries: test cases from issue 3965 658 // aligned 659 var _ = T1{ 660 a: x, 661 b: y, 662 cccccccccccccccccccc: z, 663 } 664 665 // not aligned 666 var _ = T2{ 667 a: x, 668 b: y, 669 ccccccccccccccccccccc: z, 670 } 671 672 // aligned 673 var _ = T3{ 674 aaaaaaaaaaaaaaaaaaaa: x, 675 b: y, 676 c: z, 677 } 678 679 // not aligned 680 var _ = T4{ 681 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: x, 682 b: y, 683 c: z, 684 } 685 686 func _() { 687 var _ = T{ 688 a, // must introduce trailing comma 689 } 690 } 691 692 // formatting of function results 693 func _() func() {} 694 func _() func(int) { return nil } 695 func _() func(int) int { return nil } 696 func _() func(int) func(int) func() { return nil } 697 698 // formatting of consecutive single-line functions 699 func _() {} 700 func _() {} 701 func _() {} 702 703 func _() {} // an empty line before this function 704 func _() {} 705 func _() {} 706 707 func _() { f(1, 2, 3) } 708 func _(x int) int { y := x; return y + 1 } 709 func _() int { type T struct{}; var x T; return x } 710 711 // these must remain multi-line since they are multi-line in the source 712 func _() { 713 f(1, 2, 3) 714 } 715 func _(x int) int { 716 y := x 717 return y + 1 718 } 719 func _() int { 720 type T struct{} 721 var x T 722 return x 723 } 724 725 // making function declarations safe for new semicolon rules 726 func _() { /* multi-line func because of comment */ 727 } 728 729 func _() { 730 /* multi-line func because block is on multiple lines */ 731 } 732 733 // ellipsis parameters 734 func _(...int) 735 func _(...*int) 736 func _(...[]int) 737 func _(...struct{}) 738 func _(bool, ...interface{}) 739 func _(bool, ...func()) 740 func _(bool, ...func(...int)) 741 func _(bool, ...map[string]int) 742 func _(bool, ...chan int) 743 744 func _(b bool, x ...int) 745 func _(b bool, x ...*int) 746 func _(b bool, x ...[]int) 747 func _(b bool, x ...struct{}) 748 func _(x ...interface{}) 749 func _(x ...func()) 750 func _(x ...func(...int)) 751 func _(x ...map[string]int) 752 func _(x ...chan int) 753 754 // these parameter lists must remain multi-line since they are multi-line in the source 755 func _(bool, 756 int) { 757 } 758 func _(x bool, 759 y int) { 760 } 761 func _(x, 762 y bool) { 763 } 764 func _(bool, // comment 765 int) { 766 } 767 func _(x bool, // comment 768 y int) { 769 } 770 func _(x, // comment 771 y bool) { 772 } 773 func _(bool, // comment 774 // comment 775 int) { 776 } 777 func _(x bool, // comment 778 // comment 779 y int) { 780 } 781 func _(x, // comment 782 // comment 783 y bool) { 784 } 785 func _(bool, 786 // comment 787 int) { 788 } 789 func _(x bool, 790 // comment 791 y int) { 792 } 793 func _(x, 794 // comment 795 y bool) { 796 } 797 func _(x, // comment 798 y, // comment 799 z bool) { 800 } 801 func _(x, // comment 802 y, // comment 803 z bool) { 804 } 805 func _(x int, // comment 806 y float, // comment 807 z bool) { 808 } 809 810 // properly indent multi-line signatures 811 func ManageStatus(in <-chan *Status, req <-chan Request, 812 stat chan<- *TargetInfo, 813 TargetHistorySize int) { 814 } 815 816 func MultiLineSignature0( 817 a, b, c int, 818 ) { 819 } 820 821 func MultiLineSignature1( 822 a, b, c int, 823 u, v, w float, 824 ) { 825 } 826 827 func MultiLineSignature2( 828 a, b, 829 c int, 830 ) { 831 } 832 833 func MultiLineSignature3( 834 a, b, 835 c int, u, v, 836 w float, 837 x ...int) { 838 } 839 840 func MultiLineSignature4( 841 a, b, c int, 842 u, v, 843 w float, 844 x ...int) { 845 } 846 847 func MultiLineSignature5( 848 a, b, c int, 849 u, v, w float, 850 p, q, 851 r string, 852 x ...int) { 853 } 854 855 // make sure it also works for methods in interfaces 856 type _ interface { 857 MultiLineSignature0( 858 a, b, c int, 859 ) 860 861 MultiLineSignature1( 862 a, b, c int, 863 u, v, w float, 864 ) 865 866 MultiLineSignature2( 867 a, b, 868 c int, 869 ) 870 871 MultiLineSignature3( 872 a, b, 873 c int, u, v, 874 w float, 875 x ...int) 876 877 MultiLineSignature4( 878 a, b, c int, 879 u, v, 880 w float, 881 x ...int) 882 883 MultiLineSignature5( 884 a, b, c int, 885 u, v, w float, 886 p, q, 887 r string, 888 x ...int) 889 } 890 891 // omit superfluous parentheses in parameter lists 892 func _(int) 893 func _(int) 894 func _(x int) 895 func _(x int) 896 func _(x, y int) 897 func _(x, y int) 898 899 func _() int 900 func _() int 901 func _() int 902 903 func _() (x int) 904 func _() (x int) 905 func _() (x int) 906 907 // special cases: some channel types require parentheses 908 func _(x chan (<-chan int)) 909 func _(x chan (<-chan int)) 910 func _(x chan (<-chan int)) 911 912 func _(x chan<- (chan int)) 913 func _(x chan<- (chan int)) 914 func _(x chan<- (chan int)) 915 916 // don't introduce comma after last parameter if the closing ) is on the same line 917 // even if the parameter type itself is multi-line (test cases from issue 4533) 918 func _(...interface{}) 919 func _(...interface { 920 m() 921 n() 922 }) // no extra comma between } and ) 923 924 func (t *T) _(...interface{}) 925 func (t *T) _(...interface { 926 m() 927 n() 928 }) // no extra comma between } and ) 929 930 func _(interface{}) 931 func _(interface { 932 m() 933 }) // no extra comma between } and ) 934 935 func _(struct{}) 936 func _(struct { 937 x int 938 y int 939 }) // no extra comma between } and )