1// run 2 3// Copyright 2009 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 big vs. small, pointer vs. value interface methods. 8 9package main 10 11type I interface { M() int64 } 12 13type BigPtr struct { a, b, c, d int64 } 14func (z *BigPtr) M() int64 { return z.a+z.b+z.c+z.d } 15 16type SmallPtr struct { a int32 } 17func (z *SmallPtr) M() int64 { return int64(z.a) } 18 19type IntPtr int32 20func (z *IntPtr) M() int64 { return int64(*z) } 21 22var bad bool 23 24func test(name string, i I) { 25 m := i.M() 26 if m != 12345 { 27 println(name, m) 28 bad = true 29 } 30} 31 32func ptrs() { 33 var bigptr BigPtr = BigPtr{ 10000, 2000, 300, 45 } 34 var smallptr SmallPtr = SmallPtr{ 12345 } 35 var intptr IntPtr = 12345 36 37// test("bigptr", bigptr) 38 test("&bigptr", &bigptr) 39// test("smallptr", smallptr) 40 test("&smallptr", &smallptr) 41// test("intptr", intptr) 42 test("&intptr", &intptr) 43} 44 45type Big struct { a, b, c, d int64 } 46func (z Big) M() int64 { return z.a+z.b+z.c+z.d } 47 48type Small struct { a int32 } 49func (z Small) M() int64 { return int64(z.a) } 50 51type Int int32 52func (z Int) M() int64 { return int64(z) } 53 54func nonptrs() { 55 var big Big = Big{ 10000, 2000, 300, 45 } 56 var small Small = Small{ 12345 } 57 var int Int = 12345 58 59 test("big", big) 60 test("&big", &big) 61 test("small", small) 62 test("&small", &small) 63 test("int", int) 64 test("&int", &int) 65} 66 67func main() { 68 ptrs() 69 nonptrs() 70 71 if bad { 72 println("BUG: interface4") 73 } 74} 75