另客网go项目公用的代码库

gen-enc-chan.go.tmpl 647B

12345678910111213141516171819202122232425262728
  1. {{.Label}}:
  2. switch timeout{{.Sfx}} := z.EncBasicHandle().ChanRecvTimeout; {
  3. case timeout{{.Sfx}} == 0: // only consume available
  4. for {
  5. select {
  6. case b{{.Sfx}} := <-{{.Chan}}:
  7. {{ .Slice }} = append({{.Slice}}, b{{.Sfx}})
  8. default:
  9. break {{.Label}}
  10. }
  11. }
  12. case timeout{{.Sfx}} > 0: // consume until timeout
  13. tt{{.Sfx}} := time.NewTimer(timeout{{.Sfx}})
  14. for {
  15. select {
  16. case b{{.Sfx}} := <-{{.Chan}}:
  17. {{.Slice}} = append({{.Slice}}, b{{.Sfx}})
  18. case <-tt{{.Sfx}}.C:
  19. // close(tt.C)
  20. break {{.Label}}
  21. }
  22. }
  23. default: // consume until close
  24. for b{{.Sfx}} := range {{.Chan}} {
  25. {{.Slice}} = append({{.Slice}}, b{{.Sfx}})
  26. }
  27. }