github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/goroutine/testdata/goroutine.lua (about)

     1  -- example code is taken from https://tour.golang.org/concurrency/5
     2  
     3  fibs = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}
     4  
     5  function fibonacci(ch, quit)
     6    local x, y = 0, 1
     7    while true do
     8      local chosen, recv, recvOK = goroutine.select(
     9        goroutine.case("send", ch, x),
    10        goroutine.case("recv", quit)
    11      )
    12  
    13      if chosen == 1 then
    14        x, y = y, x+y
    15      elseif chosen == 2 then
    16        return
    17      end
    18    end
    19  end
    20  
    21  ch = goroutine.newchannel()
    22  quit = goroutine.newchannel()
    23  
    24  goroutine.wrap(function()
    25    for i = 1, 10 do
    26      assert(ch:recv() == fibs[i])
    27    end
    28    quit:send(nil)
    29  end)()
    30  
    31  fibonacci(ch, quit)