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