1// Copyright 2012 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// Test method expressions with arguments. 6 7package main 8 9import "./method4a" 10 11type T1 int 12 13type T2 struct { 14 f int 15} 16 17type I1 interface { 18 Sum([]int, int) int 19} 20 21type I2 interface { 22 Sum(a []int, b int) int 23} 24 25func (i T1) Sum(a []int, b int) int { 26 r := int(i) + b 27 for _, v := range a { 28 r += v 29 } 30 return r 31} 32 33func (p *T2) Sum(a []int, b int) int { 34 r := p.f + b 35 for _, v := range a { 36 r += v 37 } 38 return r 39} 40 41func eq(v1, v2 int) { 42 if v1 != v2 { 43 panic(0) 44 } 45} 46 47func main() { 48 a := []int{1, 2, 3} 49 t1 := T1(4) 50 t2 := &T2{4} 51 52 eq(t1.Sum(a, 5), 15) 53 eq(t2.Sum(a, 6), 16) 54 55 eq(T1.Sum(t1, a, 7), 17) 56 eq((*T2).Sum(t2, a, 8), 18) 57 58 f1 := T1.Sum 59 eq(f1(t1, a, 9), 19) 60 f2 := (*T2).Sum 61 eq(f2(t2, a, 10), 20) 62 63 eq(I1.Sum(t1, a, 11), 21) 64 eq(I1.Sum(t2, a, 12), 22) 65 66 f3 := I1.Sum 67 eq(f3(t1, a, 13), 23) 68 eq(f3(t2, a, 14), 24) 69 70 eq(I2.Sum(t1, a, 15), 25) 71 eq(I2.Sum(t2, a, 16), 26) 72 73 f4 := I2.Sum 74 eq(f4(t1, a, 17), 27) 75 eq(f4(t2, a, 18), 28) 76 77 mt1 := method4a.T1(4) 78 mt2 := &method4a.T2{4} 79 80 eq(mt1.Sum(a, 30), 40) 81 eq(mt2.Sum(a, 31), 41) 82 83 eq(method4a.T1.Sum(mt1, a, 32), 42) 84 eq((*method4a.T2).Sum(mt2, a, 33), 43) 85 86 g1 := method4a.T1.Sum 87 eq(g1(mt1, a, 34), 44) 88 g2 := (*method4a.T2).Sum 89 eq(g2(mt2, a, 35), 45) 90 91 eq(method4a.I1.Sum(mt1, a, 36), 46) 92 eq(method4a.I1.Sum(mt2, a, 37), 47) 93 94 g3 := method4a.I1.Sum 95 eq(g3(mt1, a, 38), 48) 96 eq(g3(mt2, a, 39), 49) 97 98 eq(method4a.I2.Sum(mt1, a, 40), 50) 99 eq(method4a.I2.Sum(mt2, a, 41), 51) 100 101 g4 := method4a.I2.Sum 102 eq(g4(mt1, a, 42), 52) 103 eq(g4(mt2, a, 43), 53) 104} 105