Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 1 | // $G $D/$F.go && $L $F.$A && ./$A.out |
| 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 | package main |
| 8 | |
| 9 | func assertequal(is, shouldbe int, msg string) { |
| 10 | if is != shouldbe { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 11 | print("assertion fail" + msg + "\n") |
| 12 | panic(1) |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
| 16 | func f1() { |
| 17 | } |
| 18 | |
| 19 | func f2(a int) { |
| 20 | } |
| 21 | |
| 22 | func f3(a, b int) int { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 23 | return a + b |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 26 | func f4(a, b int, c float64) int { |
| 27 | return (a+b)/2 + int(c) |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func f5(a int) int { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 31 | return 5 |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func f6(a int) (r int) { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 35 | return 6 |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 38 | func f7(a int) (x int, y float64) { |
| 39 | return 7, 7.0 |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 43 | func f8(a int) (x int, y float64) { |
| 44 | return 8, 8.0 |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 47 | type T struct { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 48 | x, y int |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 51 | func (t *T) m10(a int, b float64) int { |
| 52 | return (t.x + a) * (t.y + int(b)) |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 56 | func f9(a int) (in int, fl float64) { |
| 57 | i := 9 |
| 58 | f := float64(9) |
| 59 | return i, f |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | func main() { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 64 | f1() |
| 65 | f2(1) |
| 66 | r3 := f3(1, 2) |
| 67 | assertequal(r3, 3, "3") |
| 68 | r4 := f4(0, 2, 3.0) |
| 69 | assertequal(r4, 4, "4") |
| 70 | r5 := f5(1) |
| 71 | assertequal(r5, 5, "5") |
| 72 | r6 := f6(1) |
| 73 | assertequal(r6, 6, "6") |
| 74 | var r7 int |
| 75 | var s7 float64 |
| 76 | r7, s7 = f7(1) |
| 77 | assertequal(r7, 7, "r7") |
| 78 | assertequal(int(s7), 7, "s7") |
| 79 | var r8 int |
| 80 | var s8 float64 |
| 81 | r8, s8 = f8(1) |
| 82 | assertequal(r8, 8, "r8") |
| 83 | assertequal(int(s8), 8, "s8") |
| 84 | var r9 int |
| 85 | var s9 float64 |
| 86 | r9, s9 = f9(1) |
| 87 | assertequal(r9, 9, "r9") |
| 88 | assertequal(int(s9), 9, "s9") |
| 89 | var t *T = new(T) |
| 90 | t.x = 1 |
| 91 | t.y = 2 |
| 92 | r10 := t.m10(1, 3.0) |
| 93 | assertequal(r10, 10, "10") |
Rob Pike | ab34d15 | 2008-06-06 14:27:34 -0700 | [diff] [blame] | 94 | } |