1// run 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// Test functions and goroutines. 8 9package main 10 11func caller(f func(int, int) int, a, b int, c chan int) { 12 c <- f(a, b) 13} 14 15func gocall(f func(int, int) int, a, b int) int { 16 c := make(chan int) 17 go caller(f, a, b, c) 18 return <-c 19} 20 21func call(f func(int, int) int, a, b int) int { 22 return f(a, b) 23} 24 25func call1(f func(int, int) int, a, b int) int { 26 return call(f, a, b) 27} 28 29var f func(int, int) int 30 31func add(x, y int) int { 32 return x + y 33} 34 35func fn() func(int, int) int { 36 return f 37} 38 39var fc func(int, int, chan int) 40 41func addc(x, y int, c chan int) { 42 c <- x+y 43} 44 45func fnc() func(int, int, chan int) { 46 return fc 47} 48 49func three(x int) { 50 if x != 3 { 51 println("wrong val", x) 52 panic("fail") 53 } 54} 55 56var notmain func() 57 58func emptyresults() {} 59func noresults() {} 60 61var nothing func() 62 63func main() { 64 three(call(add, 1, 2)) 65 three(call1(add, 1, 2)) 66 f = add 67 three(call(f, 1, 2)) 68 three(call1(f, 1, 2)) 69 three(call(fn(), 1, 2)) 70 three(call1(fn(), 1, 2)) 71 three(call(func(a, b int) int { return a + b }, 1, 2)) 72 three(call1(func(a, b int) int { return a + b }, 1, 2)) 73 74 fc = addc 75 c := make(chan int) 76 go addc(1, 2, c) 77 three(<-c) 78 go fc(1, 2, c) 79 three(<-c) 80 go fnc()(1, 2, c) 81 three(<-c) 82 go func(a, b int, c chan int) { c <- a+b }(1, 2, c) 83 three(<-c) 84 85 emptyresults() 86 noresults() 87 nothing = emptyresults 88 nothing() 89 nothing = noresults 90 nothing() 91} 92