github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/test/method7.go (about)

     1  // run
     2  
     3  // Copyright 2017 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test forms of method expressions T.m where T is
     8  // a literal type.
     9  
    10  package main
    11  
    12  var got, want string
    13  
    14  type I interface {
    15  	m()
    16  }
    17  
    18  type S struct {
    19  }
    20  
    21  func (S) m()          { got += " m()" }
    22  func (S) m1(s string) { got += " m1(" + s + ")" }
    23  
    24  type T int
    25  
    26  func (T) m2() { got += " m2()" }
    27  
    28  func main() {
    29  	// method expressions with named receiver types
    30  	I.m(S{})
    31  	want += " m()"
    32  
    33  	S.m1(S{}, "a")
    34  	want += " m1(a)"
    35  
    36  	// method expressions with literal receiver types
    37  	f := interface{ m1(string) }.m1
    38  	f(S{}, "b")
    39  	want += " m1(b)"
    40  
    41  	interface{ m1(string) }.m1(S{}, "c")
    42  	want += " m1(c)"
    43  
    44  	x := S{}
    45  	interface{ m1(string) }.m1(x, "d")
    46  	want += " m1(d)"
    47  
    48  	g := struct{ T }.m2
    49  	_ = g
    50  	// cannot link the call below - see #22444
    51  	// g(struct{T}{})
    52  	// want += " m2()"
    53  
    54  	if got != want {
    55  		panic("got" + got + ", want" + want)
    56  	}
    57  }