Documentation ¶
Overview ¶
Package clise implements circular slice. A circular slice is a slice that have fixed size. An append to slice that has reached its length will overwrite and start again from index 0.
For example, a clise with size 5,
var c *clise.Clise = clise.New(5) c.Push(1, 2, 3, 4, 5) fmt.Println(c.Slice()) // [1 2 3 4 5]
If we push another item, it will overwrite the first index,
c.Push(6) fmt.Println(c.Slice()) // [6 2 3 4 5]
See the examples for usage of the package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clise ¶
func New ¶
New create and initialize circular slice with fixed size. It will return nil if size <= 0.
func (*Clise) MarshalJSON ¶ added in v0.39.0
MarshalJSON call Slice on c and convert it into JSON.
Example ¶
type T struct { Int int String string } var ( c = New(3) bjson []byte err error ) c.Push(1, 2, 3, 4) bjson, err = json.Marshal(c) if err != nil { fmt.Println(err) } else { fmt.Println(string(bjson)) } c.Push("Hello", "Clise", "MarshalJSON") bjson, err = json.Marshal(c) if err != nil { fmt.Println(err) } else { fmt.Println(string(bjson)) } c.Push(&T{1, "Hello"}, &T{2, "world"}) bjson, err = json.Marshal(c) if err != nil { fmt.Println(err) } else { fmt.Println(string(bjson)) }
Output: [2,3,4] ["Hello","Clise","MarshalJSON"] ["MarshalJSON",{"Int":1,"String":"Hello"},{"Int":2,"String":"world"}]
func (*Clise) Pop ¶ added in v0.24.0
func (c *Clise) Pop() (item interface{})
Pop remove the last Push()-ed item and return it to caller. It will return nil if no more item inside it.
Example ¶
var ( c *Clise = New(5) item interface{} ) c.Push(1, 2, 3, 4, 5, 6) item = c.Pop() for item != nil { fmt.Println(item) item = c.Pop() }
Output: 6 5 4 3 2
func (*Clise) RecentSlice ¶
func (c *Clise) RecentSlice() (dst []interface{})
RecentSlice return the slice from index zero until the recent item.
Example ¶
var c *Clise = New(5) c.Push(1, 2, 3) fmt.Println(c.RecentSlice()) c.Push(4, 5, 6, 7) fmt.Println(c.RecentSlice())
Output: [1 2 3] [6 7]
func (*Clise) Reset ¶
func (c *Clise) Reset()
Reset the slice, start from zero.
Example ¶
var c *Clise = New(5) c.Push(1, 2, 3, 4, 5) fmt.Println(c.Slice()) c.Reset() c.Push(1) fmt.Println(c.Slice())
Output: [1 2 3 4 5] [1]
func (*Clise) Slice ¶
func (c *Clise) Slice() (dst []interface{})
Slice return the content of circular slice as slice in the order of the last item to the recent item.
Example ¶
var c *Clise = New(5) c.Push(1, 2) fmt.Println(c.Slice()) c.Push(3, 4, 5) fmt.Println(c.Slice()) c.Push(6) fmt.Println(c.Slice()) c.Push(7, 8, 9, 10) fmt.Println(c.Slice())
Output: [1 2] [1 2 3 4 5] [2 3 4 5 6] [6 7 8 9 10]