github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/queue/example_stack_test.go (about)

     1  package queue_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/angenalZZZ/gofunc/data/queue"
     7  )
     8  
     9  // ExampleStack demonstrates the implementation of a queue stack.
    10  func Example_stack() {
    11  	// Open/create a stack.
    12  	s, err := queue.OpenStack("data_dir")
    13  	if err != nil {
    14  		fmt.Println(err)
    15  		return
    16  	}
    17  	defer s.Close()
    18  
    19  	// Push an item onto the stack.
    20  	item, err := s.Push([]byte("item value"))
    21  	if err != nil {
    22  		fmt.Println(err)
    23  		return
    24  	}
    25  
    26  	fmt.Println(item.ID)         // 1
    27  	fmt.Println(item.Key)        // [0 0 0 0 0 0 0 1]
    28  	fmt.Println(item.Value)      // [105 116 101 109 32 118 97 108 117 101]
    29  	fmt.Println(item.ToString()) // item value
    30  
    31  	// Change the item value in the stack.
    32  	item, err = s.Update(item.ID, []byte("new item value"))
    33  	if err != nil {
    34  		fmt.Println(err)
    35  		return
    36  	}
    37  
    38  	fmt.Println(item.ToString()) // new item value
    39  
    40  	// Pop an item off the stack.
    41  	popItem, err := s.Pop()
    42  	if err != nil {
    43  		fmt.Println(err)
    44  		return
    45  	}
    46  
    47  	fmt.Println(popItem.ToString()) // new item value
    48  
    49  	// Delete the stack and its database.
    50  	s.Drop()
    51  }