You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
899 B
Go
56 lines
899 B
Go
package types
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
type Slice []interface{}
|
|
|
|
// Push 後堆
|
|
func (slice *Slice) Push(value interface{}) {
|
|
*slice = append(*slice, value)
|
|
}
|
|
|
|
// Pop 後取
|
|
func (slice *Slice) Pop() (value interface{}) {
|
|
length := len(*slice)
|
|
if length > 0 {
|
|
value = (*slice)[length-1]
|
|
*slice = (*slice)[:length-1]
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
// Unshift 前堆
|
|
func (slice *Slice) Unshift(value interface{}) {
|
|
*slice = append(Slice{value}, *slice...)
|
|
}
|
|
|
|
// Shift 前取
|
|
func (slice *Slice) Shift() (value interface{}) {
|
|
length := len(*slice)
|
|
if length > 0 {
|
|
value = (*slice)[0]
|
|
*slice = (*slice)[1:length]
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
// JSON 取得json
|
|
func (slice *Slice) JSON() ([]byte, error) {
|
|
bs, err := json.Marshal(slice)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bs, nil
|
|
}
|
|
|
|
// MustJSON 強制取得json
|
|
func (slice *Slice) MustJSON() []byte {
|
|
bs, _ := slice.JSON()
|
|
return bs
|
|
}
|