github.com/searKing/golang/go@v1.2.117/go/generator/README.md (about)

     1  [![GoDoc](https://godoc.org/github.com/searKing/golang/go/go/generator?status.svg)](https://godoc.org/github.com/searKing/golang/go/go/generator)
     2  [![Report card](https://goreportcard.com/badge/github.com/searKing/golang/go/go/generator)](https://goreportcard.com/report/github.com/searKing/golang/go/go/generator) 
     3  # Generator
     4  Generator behaves like Generator in [python](https://wiki.python.org/moin/Generators) or [ES6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*), with yield and next statements.
     5  
     6  Generator function contains one or more yield statement.  
     7  Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.  
     8  Generator generators are a simple way of creating iterators. All the overhead we mentioned above are automatically handled by generators.  
     9  Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).  
    10  If desired, go to [python](https://wiki.python.org/moin/Generators) or [ES6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) for more information.  
    11  ## Example
    12  
    13  ### Golang Generators
    14  ```go
    15  package main
    16  
    17  import (
    18      "fmt"
    19  
    20      "github.com/searKing/golang/go/go/generator"
    21  )
    22  
    23  func main() {
    24      g := func(i int) *generator.Generator {
    25          return generator.GeneratorFunc(func(yield generator.Yield) {
    26              yield(i)
    27              yield(i + 10)
    28          })
    29      }
    30  
    31      gen := g(10)
    32      
    33      // WAY 1, by channel
    34      for msg := range gen.C {
    35          fmt.Println(msg)
    36      }
    37      // WAY 2, by Next
    38      //for {
    39      //	msg, ok := gen.Next()
    40      //	if !ok {
    41      //	    return
    42      //	}
    43      //	fmt.Println(msg)
    44      //}
    45  
    46      // Output:
    47      // 10
    48      // 20
    49  }
    50  ```
    51  
    52  ### Python Generators
    53  ```python
    54  # A simple generator function
    55  def generator(i):
    56      n = 1
    57      # Generator function contains yield statements
    58      yield i
    59      yield i+10
    60  
    61  # Using for loop
    62  for item in generator(10):
    63      print(item) 
    64  
    65  # Output:
    66  # 10
    67  # 20
    68  ```
    69  ### JavaScript Generators
    70  ```javascript
    71  function* generator(i) {
    72    yield i;
    73    yield i + 10;
    74  }
    75  
    76  const gen = generator(10);
    77  
    78  console.log(gen.next().value);
    79  // expected output: 10
    80  
    81  console.log(gen.next().value);
    82  // expected output: 20
    83  ```
    84  
    85  ## Download/Install
    86  
    87  The easiest way to install is to run `go get -u github.com/searKing/golang/go/go/generator`.   
    88  You can also manually git clone the repository to `$GOPATH/src/github.com/searKing/golang/go/go/generator`.
    89  
    90  ## Inspiring Generators
    91  * [Python](https://wiki.python.org/moin/Generators)
    92  * [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)