Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -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 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 9 | var c = make(chan int) |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 10 | |
| 11 | func check(a []int) { |
| 12 | for i := 0; i < len(a); i++ { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 13 | n := <-c |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 14 | if n != a[i] { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 15 | println("want", a[i], "got", n, "at", i) |
| 16 | panic("fail") |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func f() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 22 | var i, j int |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 23 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 24 | i = 1 |
| 25 | j = 2 |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 26 | f := func() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 27 | c <- i |
| 28 | i = 4 |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 29 | g := func() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 30 | c <- i |
| 31 | c <- j |
| 32 | } |
| 33 | g() |
| 34 | c <- i |
| 35 | } |
| 36 | j = 5 |
| 37 | f() |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Accumulator generator |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 41 | func accum(n int) func(int) int { |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 42 | return func(i int) int { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 43 | n += i |
| 44 | return n |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | func g(a, b func(int) int) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 49 | c <- a(2) |
| 50 | c <- b(3) |
| 51 | c <- a(4) |
| 52 | c <- b(5) |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func h() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 56 | var x8 byte = 100 |
| 57 | var x64 int64 = 200 |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 58 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 59 | c <- int(x8) |
| 60 | c <- int(x64) |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 61 | f := func(z int) { |
| 62 | g := func() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 63 | 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 Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 77 | func newfunc() func(int) int { return func(x int) int { return x } } |
Russ Cox | 9346c6d | 2009-07-28 20:01:00 -0700 | [diff] [blame] | 78 | |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 79 | |
| 80 | func main() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 81 | go f() |
| 82 | check([]int{1, 4, 5, 4}) |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 83 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 84 | a := accum(0) |
| 85 | b := accum(1) |
| 86 | go g(a, b) |
| 87 | check([]int{2, 4, 6, 9}) |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 88 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 89 | go h() |
| 90 | check([]int{100, 200, 101, 201, 500, 101, 201, 500}) |
Russ Cox | 9346c6d | 2009-07-28 20:01:00 -0700 | [diff] [blame] | 91 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 92 | x, y := newfunc(), newfunc() |
Russ Cox | 9346c6d | 2009-07-28 20:01:00 -0700 | [diff] [blame] | 93 | if x == y { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 94 | println("newfunc returned same func") |
| 95 | panic("fail") |
Russ Cox | 9346c6d | 2009-07-28 20:01:00 -0700 | [diff] [blame] | 96 | } |
| 97 | if x(1) != 1 || y(2) != 2 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 98 | println("newfunc returned broken funcs") |
| 99 | panic("fail") |
Russ Cox | 9346c6d | 2009-07-28 20:01:00 -0700 | [diff] [blame] | 100 | } |
Russ Cox | 1b31c37 | 2010-12-13 16:51:19 -0500 | [diff] [blame] | 101 | |
| 102 | ff(1) |
| 103 | } |
| 104 | |
| 105 | func ff(x int) { |
| 106 | call(func() { |
| 107 | _ = x |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | func call(func()) { |
Russ Cox | 0f4f2a6 | 2009-02-06 13:46:56 -0800 | [diff] [blame] | 112 | } |