github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/func5.go (about) 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 9 package main 10 11 func caller(f func(int, int) int, a, b int, c chan int) { 12 c <- f(a, b) 13 } 14 15 func 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 21 func call(f func(int, int) int, a, b int) int { 22 return f(a, b) 23 } 24 25 func call1(f func(int, int) int, a, b int) int { 26 return call(f, a, b) 27 } 28 29 var f func(int, int) int 30 31 func add(x, y int) int { 32 return x + y 33 } 34 35 func fn() func(int, int) int { 36 return f 37 } 38 39 var fc func(int, int, chan int) 40 41 func addc(x, y int, c chan int) { 42 c <- x+y 43 } 44 45 func fnc() func(int, int, chan int) { 46 return fc 47 } 48 49 func three(x int) { 50 if x != 3 { 51 println("wrong val", x) 52 panic("fail") 53 } 54 } 55 56 var notmain func() 57 58 func emptyresults() {} 59 func noresults() {} 60 61 var nothing func() 62 63 func 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 }