Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [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 caller(f func(int, int) int, a, b int, c chan int) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 10 | c <- f(a, b) |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 11 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 12 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 13 | func gocall(f func(int, int) int, a, b int) int { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 14 | c := make(chan int) |
| 15 | go caller(f, a, b, c) |
| 16 | return <-c |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func call(f func(int, int) int, a, b int) int { |
| 20 | return f(a, b) |
| 21 | } |
| 22 | |
| 23 | func call1(f func(int, int) int, a, b int) int { |
| 24 | return call(f, a, b) |
| 25 | } |
| 26 | |
| 27 | var f func(int, int) int |
| 28 | |
| 29 | func add(x, y int) int { |
| 30 | return x + y |
| 31 | } |
| 32 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 33 | func fn() func(int, int) int { |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 34 | return f |
| 35 | } |
| 36 | |
| 37 | var fc func(int, int, chan int) |
| 38 | |
| 39 | func addc(x, y int, c chan int) { |
| 40 | c <- x+y |
| 41 | } |
| 42 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 43 | func fnc() func(int, int, chan int) { |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 44 | return fc |
| 45 | } |
| 46 | |
| 47 | func three(x int) { |
| 48 | if x != 3 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 49 | println("wrong val", x) |
| 50 | panic("fail") |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | var notmain func() |
| 55 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 56 | func emptyresults() {} |
| 57 | func noresults() {} |
Rob Pike | 5e11bb2 | 2009-09-14 13:09:53 -0700 | [diff] [blame] | 58 | |
| 59 | var nothing func() |
| 60 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 61 | func main() { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 62 | three(call(add, 1, 2)) |
| 63 | three(call1(add, 1, 2)) |
| 64 | f = add |
| 65 | three(call(f, 1, 2)) |
| 66 | three(call1(f, 1, 2)) |
| 67 | three(call(fn(), 1, 2)) |
| 68 | three(call1(fn(), 1, 2)) |
| 69 | three(call(func(a, b int) int { return a + b }, 1, 2)) |
| 70 | three(call1(func(a, b int) int { return a + b }, 1, 2)) |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 71 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 72 | fc = addc |
| 73 | c := make(chan int) |
| 74 | go addc(1, 2, c) |
| 75 | three(<-c) |
| 76 | go fc(1, 2, c) |
| 77 | three(<-c) |
| 78 | go fnc()(1, 2, c) |
| 79 | three(<-c) |
| 80 | go func(a, b int, c chan int) { c <- a+b }(1, 2, c) |
| 81 | three(<-c) |
Rob Pike | 5e11bb2 | 2009-09-14 13:09:53 -0700 | [diff] [blame] | 82 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 83 | emptyresults() |
| 84 | noresults() |
| 85 | nothing = emptyresults |
| 86 | nothing() |
| 87 | nothing = noresults |
| 88 | nothing() |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 89 | } |