blob: e27825c2b281cdaff880ca7b4f502556668da73b [file] [log] [blame]
Russ Cox4cf77112009-01-30 14:39:31 -08001// $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
7package main
8
9func caller(f func(int, int) int, a, b int, c chan int) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070010 c <- f(a, b)
Russ Cox4cf77112009-01-30 14:39:31 -080011}
Russ Cox00f9f0c2010-03-30 10:34:57 -070012
Russ Cox4cf77112009-01-30 14:39:31 -080013func gocall(f func(int, int) int, a, b int) int {
Russ Cox00f9f0c2010-03-30 10:34:57 -070014 c := make(chan int)
15 go caller(f, a, b, c)
16 return <-c
Russ Cox4cf77112009-01-30 14:39:31 -080017}
18
19func call(f func(int, int) int, a, b int) int {
20 return f(a, b)
21}
22
23func call1(f func(int, int) int, a, b int) int {
24 return call(f, a, b)
25}
26
27var f func(int, int) int
28
29func add(x, y int) int {
30 return x + y
31}
32
Russ Cox00f9f0c2010-03-30 10:34:57 -070033func fn() func(int, int) int {
Russ Cox4cf77112009-01-30 14:39:31 -080034 return f
35}
36
37var fc func(int, int, chan int)
38
39func addc(x, y int, c chan int) {
40 c <- x+y
41}
42
Russ Cox00f9f0c2010-03-30 10:34:57 -070043func fnc() func(int, int, chan int) {
Russ Cox4cf77112009-01-30 14:39:31 -080044 return fc
45}
46
47func three(x int) {
48 if x != 3 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070049 println("wrong val", x)
50 panic("fail")
Russ Cox4cf77112009-01-30 14:39:31 -080051 }
52}
53
54var notmain func()
55
Russ Cox00f9f0c2010-03-30 10:34:57 -070056func emptyresults() {}
57func noresults() {}
Rob Pike5e11bb22009-09-14 13:09:53 -070058
59var nothing func()
60
Russ Cox4cf77112009-01-30 14:39:31 -080061func main() {
Russ Cox00f9f0c2010-03-30 10:34:57 -070062 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 Cox4cf77112009-01-30 14:39:31 -080071
Russ Cox00f9f0c2010-03-30 10:34:57 -070072 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 Pike5e11bb22009-09-14 13:09:53 -070082
Russ Cox00f9f0c2010-03-30 10:34:57 -070083 emptyresults()
84 noresults()
85 nothing = emptyresults
86 nothing()
87 nothing = noresults
88 nothing()
Russ Cox4cf77112009-01-30 14:39:31 -080089}