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