GoLang Generator::send

request it (380)
GoLang replacement for PHP's Generator::send [edit | history]



Do you know a GoLang replacement for PHP's Generator::send? Write it!

PHP Generator::send

PHP original manual for Generator::send [ show | php.net ]

Generator::send

(PHP 5 >= 5.5.0, PHP 7)

Generator::sendSend a value to the generator

Description

public mixed Generator::send ( mixed $value )

Sends the given value to the generator as the result of the current yield expression and resumes execution of the generator.

If the generator is not at a yield expression when this method is called, it will first be let to advance to the first yield expression before sending the value. As such it is not necessary to "prime" PHP generators with a Generator::next() call (like it is done in Python).

Parameters

value

Value to send into the generator. This value will be the return value of the yield expression the generator is currently at.

Return Values

Returns the yielded value.

Examples

Example #1 Using Generator::send() to inject values

<?php
function printer() {
    echo 
"I'm printer!".PHP_EOL;
    while (
true) {
        
$string yield;
        echo 
$string.PHP_EOL;
    }
}

$printer printer();
$printer->send('Hello world!');
$printer->send('Bye world!');
?>

The above example will output:

I'm printer!
Hello world!
Bye world!