Skip to content

Instantly share code, notes, and snippets.

@scoulondre
Created July 11, 2014 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scoulondre/9bfd3dee7ee236beaaa1 to your computer and use it in GitHub Desktop.
Save scoulondre/9bfd3dee7ee236beaaa1 to your computer and use it in GitHub Desktop.
Go syntax in Python Goless
Using Goless: https://github.com/rgalanakis/goless
##########################################################
from goless import chan, select, rcase, go
import time
c1 = chan()
c2 = chan()
go(lambda: time.sleep(1) or c1.send('one'))
go(lambda: time.sleep(2) or c1.send('two'))
for i in range(2):
case, val = select([rcase(c1), rcase(c2)])
print(val)
##########################################################
Equivalent to https://gobyexample.com/select
package main
import "time"
import "fmt"
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(time.Second * 1)
c1 <- "one"
}()
go func() {
time.Sleep(time.Second * 2)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment