example_test.go 813 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package g_test
  2. import (
  3. "fmt"
  4. g "leafstalk/module/go"
  5. "time"
  6. )
  7. func Example() {
  8. d := g.New(10)
  9. // go 1
  10. var res int
  11. d.Go(func() {
  12. fmt.Println("1 + 1 = ?")
  13. res = 1 + 1
  14. }, func() {
  15. fmt.Println(res)
  16. })
  17. d.Cb(<-d.ChanCb)
  18. // go 2
  19. d.Go(func() {
  20. fmt.Print("My name is ")
  21. }, func() {
  22. fmt.Println("Leaf")
  23. })
  24. d.Close()
  25. // Output:
  26. // 1 + 1 = ?
  27. // 2
  28. // My name is Leaf
  29. }
  30. func ExampleLinearContext() {
  31. d := g.New(10)
  32. // parallel
  33. d.Go(func() {
  34. time.Sleep(time.Second / 2)
  35. fmt.Println("1")
  36. }, nil)
  37. d.Go(func() {
  38. fmt.Println("2")
  39. }, nil)
  40. d.Cb(<-d.ChanCb)
  41. d.Cb(<-d.ChanCb)
  42. // linear
  43. c := d.NewLinearContext()
  44. c.Go(func() {
  45. time.Sleep(time.Second / 2)
  46. fmt.Println("1")
  47. }, nil)
  48. c.Go(func() {
  49. fmt.Println("2")
  50. }, nil)
  51. d.Close()
  52. // Output:
  53. // 2
  54. // 1
  55. // 1
  56. // 2
  57. }