@@ -250,6 +250,10 @@ When converting C code, run it through astyle first as a first pass
250250
251251astyle --style=java --add-brackets < x.c > x.go
252252
253+ Note: clang-tidy can force all if/while/for statement bodies to be
254+ enclosed in braces and clang-format is supposed to be better so try
255+ those next time.
256+
253257Roadmap
254258=======
255259 * make pystone.py work - DONE
@@ -387,3 +391,54 @@ Used go tool yacc and a hand built lexer in parser/lexer.go
387391
388392DONE
389393
394+ Go Module
395+ =========
396+
397+ Implement go(), chan()
398+
399+ Implement select() with
400+
401+ http://golang.org/pkg/reflect/#Select
402+
403+ Eg from the mailing list
404+
405+ // timedReceive receives on channel (which can be a chan of any type), waiting
406+ // up to timeout.
407+ //
408+ // timeout==0 means do a non-blocking receive attempt. timeout < 0 means block
409+ // forever. Other values mean block up to the timeout.
410+ //
411+ // Returns (value, nil) on success, (nil, Timeout) on timeout, (nil, closeErr()) on close.
412+ //
413+ func timedReceive(channel interface{}, timeout time.Duration, closeErr func() error) (interface{}, error) {
414+ cases := []reflect.SelectCase{
415+ reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(channel)},
416+ }
417+ switch {
418+ case timeout == 0: // Non-blocking receive
419+ cases = append(cases, reflect.SelectCase{Dir: reflect.SelectDefault})
420+ case timeout < 0: // Block forever, nothing to add
421+ default: // Block up to timeout
422+ cases = append(cases,
423+ reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(time.After(timeout))})
424+ }
425+ chosen, recv, recvOk := reflect.Select(cases)
426+ switch {
427+ case chosen == 0 && recvOk:
428+ return recv.Interface(), nil
429+ case chosen == 0 && !recvOk:
430+ return nil, closeErr()
431+ default:
432+ return nil, Timeout
433+ }
434+ }
435+
436+
437+
438+ My "Incoming()" function, and other places that use this pattern are now quite trivial, just a teeny bit of casting:
439+
440+ func (c *Connection) Incoming(timeout time.Duration) (Endpoint, error) {
441+ value, err := timedReceive(c.incoming, timeout, c.Error)
442+ ep, _ := value.(Endpoint)
443+ return ep, err
444+ }
0 commit comments