daily pastebin goal
11%
SHARE
TWEET

[Go] Closure demo.

cwchen Nov 8th, 2017 233 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "log"
  5. )
  6.  
  7. func num() func() int {
  8.     n := -1
  9.  
  10.     return func() int {
  11.         n += 1
  12.         return n
  13.     }
  14. }
  15.  
  16. func main() {
  17.     f := num()
  18.  
  19.     if !(f() == 0) {
  20.         log.Fatal("Wrong number")
  21.     }
  22.  
  23.     if !(f() == 1) {
  24.         log.Fatal("Wrong number")
  25.     }
  26.  
  27.     if !(f() == 2) {
  28.         log.Fatal("Wrong number")
  29.     }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top