Advertisement
cwchen

[Go] Closure demo.

Nov 8th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.31 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement