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