blob: 3033c02ed8161c0f1e4a8cd6f010fba8fbc82ff9 [file] [log] [blame]
Russ Cox0f4f2a62009-02-06 13:46:56 -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
Rob Pike325cf8e2010-03-24 16:46:53 -07009var c = make(chan int)
Russ Cox0f4f2a62009-02-06 13:46:56 -080010
11func check(a []int) {
12 for i := 0; i < len(a); i++ {
Rob Pike325cf8e2010-03-24 16:46:53 -070013 n := <-c
Russ Cox0f4f2a62009-02-06 13:46:56 -080014 if n != a[i] {
Rob Pike325cf8e2010-03-24 16:46:53 -070015 println("want", a[i], "got", n, "at", i)
16 panic("fail")
Russ Cox0f4f2a62009-02-06 13:46:56 -080017 }
18 }
19}
20
21func f() {
Rob Pike325cf8e2010-03-24 16:46:53 -070022 var i, j int
Russ Cox0f4f2a62009-02-06 13:46:56 -080023
Rob Pike325cf8e2010-03-24 16:46:53 -070024 i = 1
25 j = 2
Russ Cox0f4f2a62009-02-06 13:46:56 -080026 f := func() {
Rob Pike325cf8e2010-03-24 16:46:53 -070027 c <- i
28 i = 4
Russ Cox0f4f2a62009-02-06 13:46:56 -080029 g := func() {
Rob Pike325cf8e2010-03-24 16:46:53 -070030 c <- i
31 c <- j
32 }
33 g()
34 c <- i
35 }
36 j = 5
37 f()
Russ Cox0f4f2a62009-02-06 13:46:56 -080038}
39
40// Accumulator generator
Rob Pike325cf8e2010-03-24 16:46:53 -070041func accum(n int) func(int) int {
Russ Cox0f4f2a62009-02-06 13:46:56 -080042 return func(i int) int {
Rob Pike325cf8e2010-03-24 16:46:53 -070043 n += i
44 return n
Russ Cox0f4f2a62009-02-06 13:46:56 -080045 }
46}
47
48func g(a, b func(int) int) {
Rob Pike325cf8e2010-03-24 16:46:53 -070049 c <- a(2)
50 c <- b(3)
51 c <- a(4)
52 c <- b(5)
Russ Cox0f4f2a62009-02-06 13:46:56 -080053}
54
55func h() {
Rob Pike325cf8e2010-03-24 16:46:53 -070056 var x8 byte = 100
57 var x64 int64 = 200
Russ Cox0f4f2a62009-02-06 13:46:56 -080058
Rob Pike325cf8e2010-03-24 16:46:53 -070059 c <- int(x8)
60 c <- int(x64)
Russ Cox0f4f2a62009-02-06 13:46:56 -080061 f := func(z int) {
62 g := func() {
Rob Pike325cf8e2010-03-24 16:46:53 -070063 c <- int(x8)
64 c <- int(x64)
65 c <- z
66 }
67 g()
68 c <- int(x8)
69 c <- int(x64)
70 c <- int(z)
71 }
72 x8 = 101
73 x64 = 201
74 f(500)
Russ Cox0f4f2a62009-02-06 13:46:56 -080075}
76
Rob Pike325cf8e2010-03-24 16:46:53 -070077func newfunc() func(int) int { return func(x int) int { return x } }
Russ Cox9346c6d2009-07-28 20:01:00 -070078
Russ Cox0f4f2a62009-02-06 13:46:56 -080079
80func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070081 go f()
82 check([]int{1, 4, 5, 4})
Russ Cox0f4f2a62009-02-06 13:46:56 -080083
Rob Pike325cf8e2010-03-24 16:46:53 -070084 a := accum(0)
85 b := accum(1)
86 go g(a, b)
87 check([]int{2, 4, 6, 9})
Russ Cox0f4f2a62009-02-06 13:46:56 -080088
Rob Pike325cf8e2010-03-24 16:46:53 -070089 go h()
90 check([]int{100, 200, 101, 201, 500, 101, 201, 500})
Russ Cox9346c6d2009-07-28 20:01:00 -070091
Rob Pike325cf8e2010-03-24 16:46:53 -070092 x, y := newfunc(), newfunc()
Russ Cox9346c6d2009-07-28 20:01:00 -070093 if x == y {
Rob Pike325cf8e2010-03-24 16:46:53 -070094 println("newfunc returned same func")
95 panic("fail")
Russ Cox9346c6d2009-07-28 20:01:00 -070096 }
97 if x(1) != 1 || y(2) != 2 {
Rob Pike325cf8e2010-03-24 16:46:53 -070098 println("newfunc returned broken funcs")
99 panic("fail")
Russ Cox9346c6d2009-07-28 20:01:00 -0700100 }
Russ Cox1b31c372010-12-13 16:51:19 -0500101
102 ff(1)
103}
104
105func ff(x int) {
106 call(func() {
107 _ = x
108 })
109}
110
111func call(func()) {
Russ Cox0f4f2a62009-02-06 13:46:56 -0800112}