github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/test/test.go (about) 1 package main 2 3 // This file is here to test features of the Go compiler. 4 5 import "unicode" 6 7 type Thing struct { 8 name string 9 } 10 11 func (t Thing) String() string { 12 return t.name 13 } 14 15 type Stringer interface { 16 String() string 17 } 18 19 type Foo int 20 21 type Number int 22 23 func (n Number) Double() int { 24 return int(n) * 2 25 } 26 27 type Doubler interface { 28 Double() int 29 } 30 31 const SIX = 6 32 33 var testmap = map[string]int{"data": 3} 34 35 func main() { 36 println("Hello world from Go!") 37 println("The answer is:", calculateAnswer()) 38 println("5 ** 2 =", square(5)) 39 println("3 + 12 =", add(3, 12)) 40 println("fib(11) =", fib(11)) 41 println("sumrange(100) =", sumrange(100)) 42 println("strlen foo:", strlen("foo")) 43 44 // map 45 m := map[string]int{"answer": 42, "foo": 3} 46 readMap(m, "answer") 47 readMap(testmap, "data") 48 49 // slice 50 l := 5 51 foo := []int{1, 2, 4, 5} 52 bar := make([]int, l-2, l) 53 println("len/cap foo:", len(foo), cap(foo)) 54 println("len/cap bar:", len(bar), cap(bar)) 55 println("len/cap foo[1:2]:", len(foo[1:2]), cap(foo[1:2])) 56 println("foo[3]:", foo[3]) 57 println("sum foo:", sum(foo)) 58 println("copy foo -> bar:", copy(bar, foo)) 59 println("sum bar:", sum(bar)) 60 61 // interfaces, pointers 62 thing := &Thing{"foo"} 63 println("thing:", thing.String()) 64 printItf(5) 65 printItf(byte('x')) 66 printItf("foo") 67 printItf(*thing) 68 printItf(thing) 69 printItf(Stringer(thing)) 70 printItf(Number(3)) 71 s := Stringer(thing) 72 println("Stringer.String():", s.String()) 73 var itf interface{} = s 74 println("Stringer.(*Thing).String():", itf.(Stringer).String()) 75 76 // unusual calls 77 runFunc(hello, 5) // must be indirect to avoid obvious inlining 78 testDefer() 79 testBound(thing.String) 80 func() { 81 println("thing inside closure:", thing.String()) 82 }() 83 runFunc(func(i int) { 84 println("inside fp closure:", thing.String(), i) 85 }, 3) 86 87 // test library functions 88 println("lower to upper char:", string('h'), "->", string(unicode.ToUpper('h'))) 89 } 90 91 func runFunc(f func(int), arg int) { 92 f(arg) 93 } 94 95 func testDefer() { 96 i := 1 97 defer deferred("...run as defer", i) 98 i += 1 99 defer deferred("...run as defer", i) 100 println("deferring...") 101 } 102 103 func deferred(msg string, i int) { 104 println(msg, i) 105 } 106 107 func testBound(f func() string) { 108 println("bound method:", f()) 109 } 110 111 func readMap(m map[string]int, key string) { 112 println("map length:", len(m)) 113 println("map read:", key, "=", m[key]) 114 for k, v := range m { 115 println(" ", k, "=", v) 116 } 117 } 118 119 func hello(n int) { 120 println("hello from function pointer:", n) 121 } 122 123 func strlen(s string) int { 124 return len(s) 125 } 126 127 func printItf(val interface{}) { 128 switch val := val.(type) { 129 case Doubler: 130 println("is Doubler:", val.Double()) 131 case int: 132 println("is int:", val) 133 case byte: 134 println("is byte:", val) 135 case string: 136 println("is string:", val) 137 case Thing: 138 println("is Thing:", val.String()) 139 case *Thing: 140 println("is *Thing:", val.String()) 141 case Foo: 142 println("is Foo:", val) 143 default: 144 println("is ?") 145 } 146 } 147 148 func calculateAnswer() int { 149 seven := 7 150 return SIX * seven 151 } 152 153 func square(n int) int { 154 return n * n 155 } 156 157 func add(a, b int) int { 158 return a + b 159 } 160 161 func fib(n int) int { 162 if n <= 2 { 163 return 1 164 } 165 return fib(n-1) + fib(n-2) 166 } 167 168 func sumrange(n int) int { 169 sum := 0 170 for i := 1; i <= n; i++ { 171 sum += i 172 } 173 return sum 174 } 175 176 func sum(l []int) int { 177 sum := 0 178 for _, n := range l { 179 sum += n 180 } 181 return sum 182 }