“This is the 19th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”.

Introduction to the

Collections was a nice little queue package that I stumbled across on Github, and it was written by a Native. Nice

Existing features include:

  • Fifo queue
  • Last in first out queue
  • Priority queue
  • deque
  • Orderly Map
  • counter
  • The sorting

The disadvantage is that it has not been updated for several years, but the function can still be used 😅

Portal: github.com/chenjiandon…

The installation

Use go to download the package locally

go get github.com/chenjiandongx/collections
Copy the code

Import can be used in the code editor

import "github.com/chenjiandongx/collections"
Copy the code

use

All functions and methods are in Collections, and different functions use different methods

First in first out

First-in, first-out (FIFO) Advantages In sequential execution scenarios, ensuring that processes can be executed in a predetermined order is particularly useful in o&M automation (one script is executed before another)

Running the following code outputs a number from 0 to 9 on the console (from small to large)

var q  = Q()

func Q() *collections.Queue {
   q := collections.NewQueue()
   return q
}

func main() {
   for i :=0; i < 10; i++{ q.Put(i) }for i:=0; i <10; i++ {if item,ok:=q.Get(); ok{
         fmt.Println(item)
      }
   }
}
Copy the code

Last in, first out

Run the following code to see that the numbers are printed from large to small

func main() {
   q := collections.NewLifoQueue()
   for i := 0; i < 10; i++ {
      q.Put(i)
   }
   for i := 0; i < 10; i++ {
      if item, ok := q.Get(); ok {
         fmt.Println(item)
      }
   }
}
Copy the code

Actual combat demo

The small demo above seems to work fine, but if you add goroutine to it, it might not work as well as you want in practice, so you need to combine channe to implement it. You can submit tasks continuously, but only execute one task at a time, and only execute the next task when this task is finished

var (
   q = collections.NewQueue()
)

func main() {
   go QueueApi()
   Web()
}

func Web() {
   r := gin.Default()
   r.GET("/api/devops".func(c *gin.Context) {

      args1 := c.Query("args1")
      args2 := c.Query("args2")
      data := map[string]string{
         "parameters1": args1,
         "parameters2": args2,
      }
      q.Put(data)

      c.JSON(http.StatusOK, gin.H{
         "message": "commit success"."data":    "ok"."code":    200,
      })
   })

   r.Run(": 8080")
}

func QueueApi() {
   fmt.Println("Start listening on queue....")
   for {
      select {
      case <-time.After(time.Second * 5): if item, ok := q.Get(); Ok {result := item.(map[string]string) var wg sync.waitgroup Wg.add (1) go func() {defer wg.done () //TODO time.Sleep(10 * time.Second) fmt.Println(result) }() wg.Wait() } } } }Copy the code

GET interface/API/Devops receives submission parameters and can continuously submit. However, QueueApi checks data every 5 seconds. When data is found in the queue, only one data is executed. Continue the loop

# curl --location --request GET'localhost:8080? args1=hello&args2=world'
Copy the code

conclusion

The power of code is still very strong, although the package is not well-known and the author is not famous, but the package is still very good (practical).

As for the other features of the package, you can explore them yourself