github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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 // use the formatted output rather than the input to decide when to align 413 // (was issue 4505) 414 const ( 415 short = 2 * ( 416 1 + 2) 417 aMuchLongerName = 3 418 ) 419 420 var ( 421 short = X{ 422 } 423 aMuchLongerName = X{} 424 425 x1 = X{} // foo 426 x2 = X{ 427 } // foo 428 ) 429 430 func _() { 431 type ( 432 xxxxxx int 433 x float 434 xxx string 435 xxxxx []x 436 xx struct{} 437 xxxxxxx struct { 438 _, _ int 439 _ float 440 } 441 xxxx chan<- string 442 ) 443 } 444 445 // alignment of "=" in consecutive lines (extended example from issue 1414) 446 const ( 447 umax uint = ^uint(0) // maximum value for a uint 448 bpu = 1 << (5 + umax>>63) // bits per uint 449 foo 450 bar = -1 451 ) 452 453 // typical enum 454 const ( 455 a MyType = iota 456 abcd 457 b 458 c 459 def 460 ) 461 462 // excerpt from godoc.go 463 var ( 464 goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory") 465 testDir = flag.String("testdir", "", "Go root subdirectory - for testing only (faster startups)") 466 pkgPath = flag.String("path", "", "additional package directories (colon-separated)") 467 filter = flag.String("filter", "", "filter file containing permitted package directory paths") 468 filterMin = flag.Int("filter_minutes", 0, "filter file update interval in minutes; disabled if <= 0") 469 filterDelay delayTime // actual filter update interval in minutes; usually filterDelay == filterMin, but filterDelay may back off exponentially 470 ) 471 472 473 // formatting of structs 474 type _ struct{} 475 476 type _ struct{ /* this comment should be visible */ } 477 478 type _ struct{ 479 // this comment should be visible and properly indented 480 } 481 482 type _ struct { // this comment must not change indentation 483 f int 484 f, ff, fff, ffff int 485 } 486 487 type _ struct { 488 string 489 } 490 491 type _ struct { 492 string // comment 493 } 494 495 type _ struct { 496 string "tag" 497 } 498 499 type _ struct { 500 string "tag" // comment 501 } 502 503 type _ struct { 504 f int 505 } 506 507 type _ struct { 508 f int // comment 509 } 510 511 type _ struct { 512 f int "tag" 513 } 514 515 type _ struct { 516 f int "tag" // comment 517 } 518 519 type _ struct { 520 bool 521 a, b, c int 522 int "tag" 523 ES // comment 524 float "tag" // comment 525 f int // comment 526 f, ff, fff, ffff int // comment 527 g float "tag" 528 h float "tag" // comment 529 } 530 531 type _ struct { a, b, 532 c, d int // this line should be indented 533 u, v, w, x float // this line should be indented 534 p, q, 535 r, s float // this line should be indented 536 } 537 538 539 // difficult cases 540 type _ struct { 541 bool // comment 542 text []byte // comment 543 } 544 545 546 // formatting of interfaces 547 type EI interface{} 548 549 type _ interface { 550 EI 551 } 552 553 type _ interface { 554 f() 555 fffff() 556 } 557 558 type _ interface { 559 EI 560 f() 561 fffffg() 562 } 563 564 type _ interface { // this comment must not change indentation 565 EI // here's a comment 566 f() // no blank between identifier and () 567 fffff() // no blank between identifier and () 568 gggggggggggg(x, y, z int) () // hurray 569 } 570 571 572 // formatting of variable declarations 573 func _() { 574 type day struct { n int; short, long string } 575 var ( 576 Sunday = day{ 0, "SUN", "Sunday" } 577 Monday = day{ 1, "MON", "Monday" } 578 Tuesday = day{ 2, "TUE", "Tuesday" } 579 Wednesday = day{ 3, "WED", "Wednesday" } 580 Thursday = day{ 4, "THU", "Thursday" } 581 Friday = day{ 5, "FRI", "Friday" } 582 Saturday = day{ 6, "SAT", "Saturday" } 583 ) 584 } 585 586 587 // formatting of multi-line variable declarations 588 var a1, b1, c1 int // all on one line 589 590 var a2, b2, 591 c2 int // this line should be indented 592 593 var (a3, b3, 594 c3, d3 int // this line should be indented 595 a4, b4, c4 int // this line should be indented 596 ) 597 598 // Test case from issue 3304: multi-line declarations must end 599 // a formatting section and not influence indentation of the 600 // next line. 601 var ( 602 minRefreshTimeSec = flag.Int64("min_refresh_time_sec", 604800, 603 "minimum time window between two refreshes for a given user.") 604 x = flag.Int64("refresh_user_rollout_percent", 100, 605 "temporary flag to ramp up the refresh user rpc") 606 aVeryLongVariableName = stats.GetVarInt("refresh-user-count") 607 ) 608 609 func _() { 610 var privateKey2 = &Block{Type: "RSA PRIVATE KEY", 611 Headers: map[string]string{}, 612 Bytes: []uint8{0x30, 0x82, 0x1, 0x3a, 0x2, 0x1, 0x0, 0x2, 613 0x41, 0x0, 0xb2, 0x99, 0xf, 0x49, 0xc4, 0x7d, 0xfa, 0x8c, 614 0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0x3b, 0x6a, 0x13, 615 0x64, 0x2b, 0x23, 0xf2, 0x8b, 0x0, 0x3b, 0xfb, 0x97, 0x79, 616 }, 617 } 618 } 619 620 621 func _() { 622 var Universe = Scope { 623 Names: map[string]*Ident { 624 // basic types 625 "bool": nil, 626 "byte": nil, 627 "int8": nil, 628 "int16": nil, 629 "int32": nil, 630 "int64": nil, 631 "uint8": nil, 632 "uint16": nil, 633 "uint32": nil, 634 "uint64": nil, 635 "float32": nil, 636 "float64": nil, 637 "string": nil, 638 639 // convenience types 640 "int": nil, 641 "uint": nil, 642 "uintptr": nil, 643 "float": nil, 644 645 // constants 646 "false": nil, 647 "true": nil, 648 "iota": nil, 649 "nil": nil, 650 651 // functions 652 "cap": nil, 653 "len": nil, 654 "new": nil, 655 "make": nil, 656 "panic": nil, 657 "panicln": nil, 658 "print": nil, 659 "println": nil, 660 }, 661 } 662 } 663 664 665 // alignment of map composite entries 666 var _ = map[int]int{ 667 // small key sizes: always align even if size ratios are large 668 a: a, 669 abcdefghabcdefgh: a, 670 ab: a, 671 abc: a, 672 abcdefgabcdefg: a, 673 abcd: a, 674 abcde: a, 675 abcdef: a, 676 677 // mixed key sizes: align when key sizes change within accepted ratio 678 abcdefgh: a, 679 abcdefghabcdefg: a, 680 abcdefghij: a, 681 abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // outlier - do not align with previous line 682 abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // align with previous line 683 684 ab: a, // do not align with previous line 685 abcde: a, // align with previous line 686 } 687 688 // alignment of map composite entries: test cases from issue 3965 689 // aligned 690 var _ = T1{ 691 a: x, 692 b: y, 693 cccccccccccccccccccc: z, 694 } 695 696 // not aligned 697 var _ = T2{ 698 a: x, 699 b: y, 700 ccccccccccccccccccccc: z, 701 } 702 703 // aligned 704 var _ = T3{ 705 aaaaaaaaaaaaaaaaaaaa: x, 706 b: y, 707 c: z, 708 } 709 710 // not aligned 711 var _ = T4{ 712 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: x, 713 b: y, 714 c: z, 715 } 716 717 718 func _() { 719 var _ = T{ 720 a, // must introduce trailing comma 721 } 722 } 723 724 725 // formatting of function results 726 func _() func() {} 727 func _() func(int) { return nil } 728 func _() func(int) int { return nil } 729 func _() func(int) func(int) func() { return nil } 730 731 732 // formatting of consecutive single-line functions 733 func _() {} 734 func _() {} 735 func _() {} 736 737 func _() {} // an empty line before this function 738 func _() {} 739 func _() {} 740 741 func _() { f(1, 2, 3) } 742 func _(x int) int { y := x; return y+1 } 743 func _() int { type T struct{}; var x T; return x } 744 745 // these must remain multi-line since they are multi-line in the source 746 func _() { 747 f(1, 2, 3) 748 } 749 func _(x int) int { 750 y := x; return y+1 751 } 752 func _() int { 753 type T struct{}; var x T; return x 754 } 755 756 757 // making function declarations safe for new semicolon rules 758 func _() { /* single-line function because of "short-ish" comment */ } 759 func _() { /* multi-line function because of "long-ish" comment - much more comment text is following here */ /* and more */ } 760 761 func _() { 762 /* multi-line func because block is on multiple lines */ } 763 764 765 // ellipsis parameters 766 func _(...int) 767 func _(...*int) 768 func _(...[]int) 769 func _(...struct{}) 770 func _(bool, ...interface{}) 771 func _(bool, ...func()) 772 func _(bool, ...func(...int)) 773 func _(bool, ...map[string]int) 774 func _(bool, ...chan int) 775 776 func _(b bool, x ...int) 777 func _(b bool, x ...*int) 778 func _(b bool, x ...[]int) 779 func _(b bool, x ...struct{}) 780 func _(x ...interface{}) 781 func _(x ...func()) 782 func _(x ...func(...int)) 783 func _(x ...map[string]int) 784 func _(x ...chan int) 785 786 787 // these parameter lists must remain multi-line since they are multi-line in the source 788 func _(bool, 789 int) { 790 } 791 func _(x bool, 792 y int) { 793 } 794 func _(x, 795 y bool) { 796 } 797 func _(bool, // comment 798 int) { 799 } 800 func _(x bool, // comment 801 y int) { 802 } 803 func _(x, // comment 804 y bool) { 805 } 806 func _(bool, // comment 807 // comment 808 int) { 809 } 810 func _(x bool, // comment 811 // comment 812 y int) { 813 } 814 func _(x, // comment 815 // comment 816 y bool) { 817 } 818 func _(bool, 819 // comment 820 int) { 821 } 822 func _(x bool, 823 // comment 824 y int) { 825 } 826 func _(x, 827 // comment 828 y bool) { 829 } 830 func _(x, // comment 831 y,// comment 832 z bool) { 833 } 834 func _(x, // comment 835 y,// comment 836 z bool) { 837 } 838 func _(x int, // comment 839 y float, // comment 840 z bool) { 841 } 842 843 844 // properly indent multi-line signatures 845 func ManageStatus(in <-chan *Status, req <-chan Request, 846 stat chan<- *TargetInfo, 847 TargetHistorySize int) { 848 } 849 850 func MultiLineSignature0( 851 a, b, c int, 852 ) {} 853 854 func MultiLineSignature1( 855 a, b, c int, 856 u, v, w float, 857 ) {} 858 859 func MultiLineSignature2( 860 a, b, 861 c int, 862 ) {} 863 864 func MultiLineSignature3( 865 a, b, 866 c int, u, v, 867 w float, 868 x ...int) {} 869 870 func MultiLineSignature4( 871 a, b, c int, 872 u, v, 873 w float, 874 x ...int) {} 875 876 func MultiLineSignature5( 877 a, b, c int, 878 u, v, w float, 879 p, q, 880 r string, 881 x ...int) {} 882 883 // make sure it also works for methods in interfaces 884 type _ interface { 885 MultiLineSignature0( 886 a, b, c int, 887 ) 888 889 MultiLineSignature1( 890 a, b, c int, 891 u, v, w float, 892 ) 893 894 MultiLineSignature2( 895 a, b, 896 c int, 897 ) 898 899 MultiLineSignature3( 900 a, b, 901 c int, u, v, 902 w float, 903 x ...int) 904 905 MultiLineSignature4( 906 a, b, c int, 907 u, v, 908 w float, 909 x ...int) 910 911 MultiLineSignature5( 912 a, b, c int, 913 u, v, w float, 914 p, q, 915 r string, 916 x ...int) 917 } 918 919 // omit superfluous parentheses in parameter lists 920 func _((int)) 921 func _((((((int)))))) 922 func _(x (int)) 923 func _(x (((((int)))))) 924 func _(x, y (int)) 925 func _(x, y (((((int)))))) 926 927 func _() (int) 928 func _() ((int)) 929 func _() ((((((int)))))) 930 931 func _() (x int) 932 func _() (x (int)) 933 func _() (x (((((int)))))) 934 935 // special cases: some channel types require parentheses 936 func _(x chan(<-chan int)) 937 func _(x (chan(<-chan int))) 938 func _(x ((((chan(<-chan int)))))) 939 940 func _(x chan<-(chan int)) 941 func _(x (chan<-(chan int))) 942 func _(x ((((chan<-(chan int)))))) 943 944 // don't introduce comma after last parameter if the closing ) is on the same line 945 // even if the parameter type itself is multi-line (test cases from issue 4533) 946 func _(...interface{}) 947 func _(...interface { 948 m() 949 n() 950 }) // no extra comma between } and ) 951 952 func (t *T) _(...interface{}) 953 func (t *T) _(...interface { 954 m() 955 n() 956 }) // no extra comma between } and ) 957 958 func _(interface{}) 959 func _(interface { 960 m() 961 }) // no extra comma between } and ) 962 963 func _(struct{}) 964 func _(struct { 965 x int 966 y int 967 }) // no extra comma between } and )