github.com/aykevl/tinygo@v0.5.0/testdata/interface.go (about)

     1  package main
     2  
     3  func main() {
     4  	thing := &Thing{"foo"}
     5  	println("thing:", thing.String())
     6  	thing.Print()
     7  	printItf(5)
     8  	printItf(byte('x'))
     9  	printItf("foo")
    10  	printItf(Foo(18))
    11  	printItf(*thing)
    12  	printItf(thing)
    13  	printItf(Stringer(thing))
    14  	printItf(Number(3))
    15  	array := Array([4]uint32{1, 7, 11, 13})
    16  	printItf(array)
    17  	printItf(ArrayStruct{3, array})
    18  	printItf(SmallPair{3, 5})
    19  	s := Stringer(thing)
    20  	println("Stringer.String():", s.String())
    21  	var itf interface{} = s
    22  	println("Stringer.(*Thing).String():", itf.(Stringer).String())
    23  
    24  	println("nested switch:", nestedSwitch('v', 3))
    25  }
    26  
    27  func printItf(val interface{}) {
    28  	switch val := val.(type) {
    29  	case Unmatched:
    30  		panic("matched the unmatchable")
    31  	case Doubler:
    32  		println("is Doubler:", val.Double())
    33  	case Tuple:
    34  		println("is Tuple:", val.Nth(0), val.Nth(1), val.Nth(2), val.Nth(3))
    35  		val.Print()
    36  	case int:
    37  		println("is int:", val)
    38  	case byte:
    39  		println("is byte:", val)
    40  	case string:
    41  		println("is string:", val)
    42  	case Thing:
    43  		println("is Thing:", val.String())
    44  	case *Thing:
    45  		println("is *Thing:", val.String())
    46  	case Foo:
    47  		println("is Foo:", val)
    48  	default:
    49  		println("is ?")
    50  	}
    51  }
    52  
    53  func nestedSwitch(verb rune, arg interface{}) bool {
    54  	switch verb {
    55  	case 'v', 's':
    56  		switch arg.(type) {
    57  		case int:
    58  			return true
    59  		}
    60  	}
    61  	return false
    62  }
    63  
    64  type Thing struct {
    65  	name string
    66  }
    67  
    68  func (t Thing) String() string {
    69  	return t.name
    70  }
    71  
    72  func (t Thing) Print() {
    73  	println("Thing.Print:", t.name)
    74  }
    75  
    76  type Stringer interface {
    77  	String() string
    78  }
    79  
    80  type Foo int
    81  
    82  type Number int
    83  
    84  func (n Number) Double() int {
    85  	return int(n) * 2
    86  }
    87  
    88  type Doubler interface {
    89  	Double() int
    90  }
    91  
    92  type Tuple interface {
    93  	Nth(int) uint32
    94  	Print()
    95  }
    96  
    97  type Array [4]uint32
    98  
    99  func (a Array) Nth(n int) uint32 {
   100  	return a[n]
   101  }
   102  
   103  func (a Array) Print() {
   104  	println("Array len:", len(a))
   105  }
   106  
   107  type ArrayStruct struct {
   108  	n int
   109  	a Array
   110  }
   111  
   112  func (a ArrayStruct) Nth(n int) uint32 {
   113  	return a.a[n]
   114  }
   115  
   116  func (a ArrayStruct) Print() {
   117  	println("ArrayStruct.Print:", len(a.a), a.n)
   118  }
   119  
   120  type SmallPair struct {
   121  	a byte
   122  	b byte
   123  }
   124  
   125  func (p SmallPair) Nth(n int) uint32 {
   126  	return uint32(int(p.a)*n + int(p.b)*n)
   127  }
   128  
   129  func (p SmallPair) Print() {
   130  	println("SmallPair.Print:", p.a, p.b)
   131  }
   132  
   133  // There is no type that matches this method.
   134  type Unmatched interface {
   135  	NeverImplementedMethod()
   136  }