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