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