github.com/MeteorsLiu/simpleMQ@v1.0.3/readme.md (about)

     1  # Simple Message Queue
     2  
     3  一个简单的,异步的,开箱即用的消息队列
     4  
     5  A simple, asynchronous and ready-to-use message queue for golang.
     6  
     7  Example(例子):
     8  
     9  ```
    10  import (
    11      "github.com/MeteorsLiu/simpleMQ/router"
    12  )
    13  
    14  ...
    15  
    16  r := router.NewRouter()
    17  // dispath a task
    18  task := r.Dispath(func () error {
    19      time.Sleep(5*time.Second)
    20      fmt.Println("Hello")
    21      return nil
    22  })
    23  
    24  // stop the task, but it will still run.
    25  task.Stop()
    26  
    27  ```
    28  
    29  
    30  If you need to wait the result
    31  
    32  ```
    33  realTask := func () (string, error) {
    34      time.Sleep(5*time.Second)
    35      return fmt.Sprintf("Hello"), nil
    36  }
    37  
    38  callback := make(chan string)
    39  // dispath a task
    40  task := r.Dispath(func () error {
    41      ret, err := realTask()
    42      if err == nil {
    43           callback <- ret
    44      }
    45      return err
    46  })
    47  
    48  result := <-callback
    49  ```
    50  
    51  Or just wait the task:
    52  
    53  ```
    54  // dispath a task
    55  task := r.Dispath(func () error {
    56      time.Sleep(5*time.Second)
    57      fmt.Println("Hello")
    58      return nil
    59  })
    60  
    61  task.Wait()
    62  ```
    63  
    64  ## Dispath the task to a specifc router path
    65  ```
    66  // dispath a task
    67  task := r.DispathPath("/red", func () error {
    68      time.Sleep(5*time.Second)
    69      fmt.Println("Hello")
    70      return nil
    71  })
    72  ```