| 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 |
type Outer struct{ *Inner } |
| 29 |
type Inner struct{ s string } |
| 30 |
|
| 31 |
func (i Inner) M() string { return i.s } |
| 32 |
|
| 33 |
func main() { |
| 34 |
// method expressions with named receiver types |
| 35 |
I.m(S{}) |
| 36 |
want += " m()" |
| 37 |
|
| 38 |
S.m1(S{}, "a") |
| 39 |
want += " m1(a)" |
| 40 |
|
| 41 |
// method expressions with literal receiver types |
| 42 |
f := interface{ m1(string) }.m1 |
| 43 |
f(S{}, "b") |
| 44 |
want += " m1(b)" |
| 45 |
|
| 46 |
interface{ m1(string) }.m1(S{}, "c") |
| 47 |
want += " m1(c)" |
| 48 |
|
| 49 |
x := S{} |
| 50 |
interface{ m1(string) }.m1(x, "d") |
| 51 |
want += " m1(d)" |
| 52 |
|
| 53 |
g := struct{ T }.m2 |
| 54 |
g(struct{ T }{}) |
| 55 |
want += " m2()" |
| 56 |
|
| 57 |
if got != want { |
| 58 |
panic("got" + got + ", want" + want) |
| 59 |
} |
| 60 |
|
| 61 |
h := (*Outer).M |
| 62 |
got := h(&Outer{&Inner{"hello"}}) |
| 63 |
want := "hello" |
| 64 |
if got != want { |
| 65 |
panic("got " + got + ", want " + want) |
| 66 |
} |
| 67 |
} |