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 ¶
- type Clise
- func (c *Clise) Close() error
- func (c *Clise) MarshalJSON() (out []byte, err error)
- func (c *Clise) Pop() (item interface{})
- func (c *Clise) Push(src ...interface{})
- func (c *Clise) RecentSlice() (dst []interface{})
- func (c *Clise) Reset()
- func (c *Clise) Slice() (dst []interface{})
- func (c *Clise) UnmarshalJSON(jsonb []byte) (err error)
- func (c *Clise) Write(b []byte) (n int, err error)
- func (c *Clise) WriteByte(b byte) error
- func (c *Clise) WriteString(s string) (n int, err error)
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]
func (*Clise) UnmarshalJSON ¶ added in v0.43.0
UnmarshalJSON unmarshal JSON array into Clise. If the size is zero, it will be set to the length of JSON array.
Example ¶
var ( clise *Clise = New(3) cases = []string{ `{"k":1}`, // Non array. `[2,3,4]`, `[2,3,4,5]`, // Array elements greater that maximum size. `["Hello","Clise","MarshalJSON"]`, `["MarshalJSON",{"Int":1,"String":"Hello"},{"Int":2,"String":"world"}]`, } rawJson string err error ) for _, rawJson = range cases { err = clise.UnmarshalJSON([]byte(rawJson)) if err != nil { fmt.Println(err) } else { fmt.Println(clise.Slice()) } }
Output: UnmarshalJSON: json: cannot unmarshal object into Go value of type []interface {} [2 3 4] [3 4 5] [Hello Clise MarshalJSON] [MarshalJSON map[Int:1 String:Hello] map[Int:2 String:world]]