github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/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 // no alignment of map composite entries if they are not the first entry on a line 719 var _ = T{0: 0} // not aligned 720 var _ = T{0: 0, // not aligned 721 1: 1, // aligned 722 22: 22, // aligned 723 333: 333, 1234: 12, 12345: 0, // first on line aligned 724 } 725 726 727 // test cases form issue 8685 728 // not aligned 729 var _ = map[int]string{1: "spring", 2: "summer", 730 3: "autumn", 4: "winter"} 731 732 // not aligned 733 var _ = map[string]string{"a": "spring", "b": "summer", 734 "c": "autumn", "d": "winter"} 735 736 // aligned 737 var _ = map[string]string{"a": "spring", 738 "b": "summer", 739 "c": "autumn", 740 "d": "winter"} 741 742 743 func _() { 744 var _ = T{ 745 a, // must introduce trailing comma 746 } 747 } 748 749 750 // formatting of function results 751 func _() func() {} 752 func _() func(int) { return nil } 753 func _() func(int) int { return nil } 754 func _() func(int) func(int) func() { return nil } 755 756 757 // formatting of consecutive single-line functions 758 func _() {} 759 func _() {} 760 func _() {} 761 762 func _() {} // an empty line before this function 763 func _() {} 764 func _() {} 765 766 func _() { f(1, 2, 3) } 767 func _(x int) int { y := x; return y+1 } 768 func _() int { type T struct{}; var x T; return x } 769 770 // these must remain multi-line since they are multi-line in the source 771 func _() { 772 f(1, 2, 3) 773 } 774 func _(x int) int { 775 y := x; return y+1 776 } 777 func _() int { 778 type T struct{}; var x T; return x 779 } 780 781 782 // making function declarations safe for new semicolon rules 783 func _() { /* single-line function because of "short-ish" comment */ } 784 func _() { /* multi-line function because of "long-ish" comment - much more comment text is following here */ /* and more */ } 785 786 func _() { 787 /* multi-line func because block is on multiple lines */ } 788 789 790 // ellipsis parameters 791 func _(...int) 792 func _(...*int) 793 func _(...[]int) 794 func _(...struct{}) 795 func _(bool, ...interface{}) 796 func _(bool, ...func()) 797 func _(bool, ...func(...int)) 798 func _(bool, ...map[string]int) 799 func _(bool, ...chan int) 800 801 func _(b bool, x ...int) 802 func _(b bool, x ...*int) 803 func _(b bool, x ...[]int) 804 func _(b bool, x ...struct{}) 805 func _(x ...interface{}) 806 func _(x ...func()) 807 func _(x ...func(...int)) 808 func _(x ...map[string]int) 809 func _(x ...chan int) 810 811 812 // these parameter lists must remain multi-line since they are multi-line in the source 813 func _(bool, 814 int) { 815 } 816 func _(x bool, 817 y int) { 818 } 819 func _(x, 820 y bool) { 821 } 822 func _(bool, // comment 823 int) { 824 } 825 func _(x bool, // comment 826 y int) { 827 } 828 func _(x, // comment 829 y bool) { 830 } 831 func _(bool, // comment 832 // comment 833 int) { 834 } 835 func _(x bool, // comment 836 // comment 837 y int) { 838 } 839 func _(x, // comment 840 // comment 841 y bool) { 842 } 843 func _(bool, 844 // comment 845 int) { 846 } 847 func _(x bool, 848 // comment 849 y int) { 850 } 851 func _(x, 852 // comment 853 y bool) { 854 } 855 func _(x, // comment 856 y,// comment 857 z bool) { 858 } 859 func _(x, // comment 860 y,// comment 861 z bool) { 862 } 863 func _(x int, // comment 864 y float, // comment 865 z bool) { 866 } 867 868 869 // properly indent multi-line signatures 870 func ManageStatus(in <-chan *Status, req <-chan Request, 871 stat chan<- *TargetInfo, 872 TargetHistorySize int) { 873 } 874 875 func MultiLineSignature0( 876 a, b, c int, 877 ) {} 878 879 func MultiLineSignature1( 880 a, b, c int, 881 u, v, w float, 882 ) {} 883 884 func MultiLineSignature2( 885 a, b, 886 c int, 887 ) {} 888 889 func MultiLineSignature3( 890 a, b, 891 c int, u, v, 892 w float, 893 x ...int) {} 894 895 func MultiLineSignature4( 896 a, b, c int, 897 u, v, 898 w float, 899 x ...int) {} 900 901 func MultiLineSignature5( 902 a, b, c int, 903 u, v, w float, 904 p, q, 905 r string, 906 x ...int) {} 907 908 // make sure it also works for methods in interfaces 909 type _ interface { 910 MultiLineSignature0( 911 a, b, c int, 912 ) 913 914 MultiLineSignature1( 915 a, b, c int, 916 u, v, w float, 917 ) 918 919 MultiLineSignature2( 920 a, b, 921 c int, 922 ) 923 924 MultiLineSignature3( 925 a, b, 926 c int, u, v, 927 w float, 928 x ...int) 929 930 MultiLineSignature4( 931 a, b, c int, 932 u, v, 933 w float, 934 x ...int) 935 936 MultiLineSignature5( 937 a, b, c int, 938 u, v, w float, 939 p, q, 940 r string, 941 x ...int) 942 } 943 944 // omit superfluous parentheses in parameter lists 945 func _((int)) 946 func _((((((int)))))) 947 func _(x (int)) 948 func _(x (((((int)))))) 949 func _(x, y (int)) 950 func _(x, y (((((int)))))) 951 952 func _() (int) 953 func _() ((int)) 954 func _() ((((((int)))))) 955 956 func _() (x int) 957 func _() (x (int)) 958 func _() (x (((((int)))))) 959 960 // special cases: some channel types require parentheses 961 func _(x chan(<-chan int)) 962 func _(x (chan(<-chan int))) 963 func _(x ((((chan(<-chan int)))))) 964 965 func _(x chan<-(chan int)) 966 func _(x (chan<-(chan int))) 967 func _(x ((((chan<-(chan int)))))) 968 969 // don't introduce comma after last parameter if the closing ) is on the same line 970 // even if the parameter type itself is multi-line (test cases from issue 4533) 971 func _(...interface{}) 972 func _(...interface { 973 m() 974 n() 975 }) // no extra comma between } and ) 976 977 func (t *T) _(...interface{}) 978 func (t *T) _(...interface { 979 m() 980 n() 981 }) // no extra comma between } and ) 982 983 func _(interface{}) 984 func _(interface { 985 m() 986 }) // no extra comma between } and ) 987 988 func _(struct{}) 989 func _(struct { 990 x int 991 y int 992 }) // no extra comma between } and )