Documentation ¶
Overview ¶
Package garray provides most commonly used array containers which also support concurrent-safe/unsafe switch feature.
Index ¶
- type Array
- func New(safe ...bool) *Array
- func NewArray(safe ...bool) *Array
- func NewArrayFrom(array []interface{}, safe ...bool) *Array
- func NewArrayFromCopy(array []interface{}, safe ...bool) *Array
- func NewArrayRange(start, end, step int, safe ...bool) *Array
- func NewArraySize(size int, cap int, safe ...bool) *Array
- func NewFrom(array []interface{}, safe ...bool) *Array
- func NewFromCopy(array []interface{}, safe ...bool) *Array
- func (a *Array) Append(value ...interface{}) *Array
- func (a *Array) At(index int) (value interface{})
- func (a *Array) Chunk(size int) [][]interface{}
- func (a *Array) Clear() *Array
- func (a *Array) Clone() (newArray *Array)
- func (a *Array) Contains(value interface{}) bool
- func (a *Array) CountValues() map[interface{}]int
- func (a *Array) DeepCopy() interface{}
- func (a *Array) Fill(startIndex int, num int, value interface{}) error
- func (a *Array) Filter(filter func(index int, value interface{}) bool) *Array
- func (a *Array) FilterEmpty() *Array
- func (a *Array) FilterNil() *Array
- func (a *Array) Get(index int) (value interface{}, found bool)
- func (a *Array) InsertAfter(index int, values ...interface{}) error
- func (a *Array) InsertBefore(index int, values ...interface{}) error
- func (a *Array) Interfaces() []interface{}
- func (a *Array) IsEmpty() bool
- func (a *Array) Iterator(f func(k int, v interface{}) bool)
- func (a *Array) IteratorAsc(f func(k int, v interface{}) bool)
- func (a *Array) IteratorDesc(f func(k int, v interface{}) bool)
- func (a *Array) Join(glue string) string
- func (a *Array) Len() int
- func (a *Array) LockFunc(f func(array []interface{})) *Array
- func (a Array) MarshalJSON() ([]byte, error)
- func (a *Array) Merge(array interface{}) *Array
- func (a *Array) Pad(size int, val interface{}) *Array
- func (a *Array) PopLeft() (value interface{}, found bool)
- func (a *Array) PopLefts(size int) []interface{}
- func (a *Array) PopRand() (value interface{}, found bool)
- func (a *Array) PopRands(size int) []interface{}
- func (a *Array) PopRight() (value interface{}, found bool)
- func (a *Array) PopRights(size int) []interface{}
- func (a *Array) PushLeft(value ...interface{}) *Array
- func (a *Array) PushRight(value ...interface{}) *Array
- func (a *Array) RLockFunc(f func(array []interface{})) *Array
- func (a *Array) Rand() (value interface{}, found bool)
- func (a *Array) Rands(size int) []interface{}
- func (a *Array) Range(start int, end ...int) []interface{}
- func (a *Array) Remove(index int) (value interface{}, found bool)
- func (a *Array) RemoveValue(value interface{}) bool
- func (a *Array) RemoveValues(values ...interface{})
- func (a *Array) Replace(array []interface{}) *Array
- func (a *Array) Reverse() *Array
- func (a *Array) Search(value interface{}) int
- func (a *Array) Set(index int, value interface{}) error
- func (a *Array) SetArray(array []interface{}) *Array
- func (a *Array) Shuffle() *Array
- func (a *Array) Slice() []interface{}
- func (a *Array) SortFunc(less func(v1, v2 interface{}) bool) *Array
- func (a *Array) String() string
- func (a *Array) SubSlice(offset int, length ...int) []interface{}
- func (a *Array) Sum() (sum int)
- func (a *Array) Unique() *Array
- func (a *Array) UnmarshalJSON(b []byte) error
- func (a *Array) UnmarshalValue(value interface{}) error
- func (a *Array) Walk(f func(value interface{}) interface{}) *Array
- type IntArray
- func (a *IntArray) Append(value ...int) *IntArray
- func (a *IntArray) At(index int) (value int)
- func (a *IntArray) Chunk(size int) [][]int
- func (a *IntArray) Clear() *IntArray
- func (a *IntArray) Clone() (newArray *IntArray)
- func (a *IntArray) Contains(value int) bool
- func (a *IntArray) CountValues() map[int]int
- func (a *IntArray) DeepCopy() interface{}
- func (a *IntArray) Fill(startIndex int, num int, value int) error
- func (a *IntArray) Filter(filter func(index int, value int) bool) *IntArray
- func (a *IntArray) FilterEmpty() *IntArray
- func (a *IntArray) Get(index int) (value int, found bool)
- func (a *IntArray) InsertAfter(index int, values ...int) error
- func (a *IntArray) InsertBefore(index int, values ...int) error
- func (a *IntArray) Interfaces() []interface{}
- func (a *IntArray) IsEmpty() bool
- func (a *IntArray) Iterator(f func(k int, v int) bool)
- func (a *IntArray) IteratorAsc(f func(k int, v int) bool)
- func (a *IntArray) IteratorDesc(f func(k int, v int) bool)
- func (a *IntArray) Join(glue string) string
- func (a *IntArray) Len() int
- func (a *IntArray) LockFunc(f func(array []int)) *IntArray
- func (a IntArray) MarshalJSON() ([]byte, error)
- func (a *IntArray) Merge(array interface{}) *IntArray
- func (a *IntArray) Pad(size int, value int) *IntArray
- func (a *IntArray) PopLeft() (value int, found bool)
- func (a *IntArray) PopLefts(size int) []int
- func (a *IntArray) PopRand() (value int, found bool)
- func (a *IntArray) PopRands(size int) []int
- func (a *IntArray) PopRight() (value int, found bool)
- func (a *IntArray) PopRights(size int) []int
- func (a *IntArray) PushLeft(value ...int) *IntArray
- func (a *IntArray) PushRight(value ...int) *IntArray
- func (a *IntArray) RLockFunc(f func(array []int)) *IntArray
- func (a *IntArray) Rand() (value int, found bool)
- func (a *IntArray) Rands(size int) []int
- func (a *IntArray) Range(start int, end ...int) []int
- func (a *IntArray) Remove(index int) (value int, found bool)
- func (a *IntArray) RemoveValue(value int) bool
- func (a *IntArray) RemoveValues(values ...int)
- func (a *IntArray) Replace(array []int) *IntArray
- func (a *IntArray) Reverse() *IntArray
- func (a *IntArray) Search(value int) int
- func (a *IntArray) Set(index int, value int) error
- func (a *IntArray) SetArray(array []int) *IntArray
- func (a *IntArray) Shuffle() *IntArray
- func (a *IntArray) Slice() []int
- func (a *IntArray) Sort(reverse ...bool) *IntArray
- func (a *IntArray) SortFunc(less func(v1, v2 int) bool) *IntArray
- func (a *IntArray) String() string
- func (a *IntArray) SubSlice(offset int, length ...int) []int
- func (a *IntArray) Sum() (sum int)
- func (a *IntArray) Unique() *IntArray
- func (a *IntArray) UnmarshalJSON(b []byte) error
- func (a *IntArray) UnmarshalValue(value interface{}) error
- func (a *IntArray) Walk(f func(value int) int) *IntArray
- type SortedArray
- func NewSortedArray(comparator func(a, b interface{}) int, safe ...bool) *SortedArray
- func NewSortedArrayFrom(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
- func NewSortedArrayFromCopy(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
- func NewSortedArrayRange(start, end, step int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
- func NewSortedArraySize(cap int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
- func (a *SortedArray) Add(values ...interface{}) *SortedArray
- func (a *SortedArray) Append(values ...interface{}) *SortedArray
- func (a *SortedArray) At(index int) (value interface{})
- func (a *SortedArray) Chunk(size int) [][]interface{}
- func (a *SortedArray) Clear() *SortedArray
- func (a *SortedArray) Clone() (newArray *SortedArray)
- func (a *SortedArray) Contains(value interface{}) bool
- func (a *SortedArray) CountValues() map[interface{}]int
- func (a *SortedArray) DeepCopy() interface{}
- func (a *SortedArray) Filter(filter func(index int, value interface{}) bool) *SortedArray
- func (a *SortedArray) FilterEmpty() *SortedArray
- func (a *SortedArray) FilterNil() *SortedArray
- func (a *SortedArray) Get(index int) (value interface{}, found bool)
- func (a *SortedArray) Interfaces() []interface{}
- func (a *SortedArray) IsEmpty() bool
- func (a *SortedArray) Iterator(f func(k int, v interface{}) bool)
- func (a *SortedArray) IteratorAsc(f func(k int, v interface{}) bool)
- func (a *SortedArray) IteratorDesc(f func(k int, v interface{}) bool)
- func (a *SortedArray) Join(glue string) string
- func (a *SortedArray) Len() int
- func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray
- func (a SortedArray) MarshalJSON() ([]byte, error)
- func (a *SortedArray) Merge(array interface{}) *SortedArray
- func (a *SortedArray) PopLeft() (value interface{}, found bool)
- func (a *SortedArray) PopLefts(size int) []interface{}
- func (a *SortedArray) PopRand() (value interface{}, found bool)
- func (a *SortedArray) PopRands(size int) []interface{}
- func (a *SortedArray) PopRight() (value interface{}, found bool)
- func (a *SortedArray) PopRights(size int) []interface{}
- func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray
- func (a *SortedArray) Rand() (value interface{}, found bool)
- func (a *SortedArray) Rands(size int) []interface{}
- func (a *SortedArray) Range(start int, end ...int) []interface{}
- func (a *SortedArray) Remove(index int) (value interface{}, found bool)
- func (a *SortedArray) RemoveValue(value interface{}) bool
- func (a *SortedArray) RemoveValues(values ...interface{})
- func (a *SortedArray) Search(value interface{}) (index int)
- func (a *SortedArray) SetArray(array []interface{}) *SortedArray
- func (a *SortedArray) SetComparator(comparator func(a, b interface{}) int)
- func (a *SortedArray) SetUnique(unique bool) *SortedArray
- func (a *SortedArray) Slice() []interface{}
- func (a *SortedArray) Sort() *SortedArray
- func (a *SortedArray) String() string
- func (a *SortedArray) SubSlice(offset int, length ...int) []interface{}
- func (a *SortedArray) Sum() (sum int)
- func (a *SortedArray) Unique() *SortedArray
- func (a *SortedArray) UnmarshalJSON(b []byte) error
- func (a *SortedArray) UnmarshalValue(value interface{}) (err error)
- func (a *SortedArray) Walk(f func(value interface{}) interface{}) *SortedArray
- type SortedIntArray
- func NewSortedIntArray(safe ...bool) *SortedIntArray
- func NewSortedIntArrayComparator(comparator func(a, b int) int, safe ...bool) *SortedIntArray
- func NewSortedIntArrayFrom(array []int, safe ...bool) *SortedIntArray
- func NewSortedIntArrayFromCopy(array []int, safe ...bool) *SortedIntArray
- func NewSortedIntArrayRange(start, end, step int, safe ...bool) *SortedIntArray
- func NewSortedIntArraySize(cap int, safe ...bool) *SortedIntArray
- func (a *SortedIntArray) Add(values ...int) *SortedIntArray
- func (a *SortedIntArray) Append(values ...int) *SortedIntArray
- func (a *SortedIntArray) At(index int) (value int)
- func (a *SortedIntArray) Chunk(size int) [][]int
- func (a *SortedIntArray) Clear() *SortedIntArray
- func (a *SortedIntArray) Clone() (newArray *SortedIntArray)
- func (a *SortedIntArray) Contains(value int) bool
- func (a *SortedIntArray) CountValues() map[int]int
- func (a *SortedIntArray) DeepCopy() interface{}
- func (a *SortedIntArray) Filter(filter func(index int, value int) bool) *SortedIntArray
- func (a *SortedIntArray) FilterEmpty() *SortedIntArray
- func (a *SortedIntArray) Get(index int) (value int, found bool)
- func (a *SortedIntArray) Interfaces() []interface{}
- func (a *SortedIntArray) IsEmpty() bool
- func (a *SortedIntArray) Iterator(f func(k int, v int) bool)
- func (a *SortedIntArray) IteratorAsc(f func(k int, v int) bool)
- func (a *SortedIntArray) IteratorDesc(f func(k int, v int) bool)
- func (a *SortedIntArray) Join(glue string) string
- func (a *SortedIntArray) Len() int
- func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray
- func (a SortedIntArray) MarshalJSON() ([]byte, error)
- func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray
- func (a *SortedIntArray) PopLeft() (value int, found bool)
- func (a *SortedIntArray) PopLefts(size int) []int
- func (a *SortedIntArray) PopRand() (value int, found bool)
- func (a *SortedIntArray) PopRands(size int) []int
- func (a *SortedIntArray) PopRight() (value int, found bool)
- func (a *SortedIntArray) PopRights(size int) []int
- func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray
- func (a *SortedIntArray) Rand() (value int, found bool)
- func (a *SortedIntArray) Rands(size int) []int
- func (a *SortedIntArray) Range(start int, end ...int) []int
- func (a *SortedIntArray) Remove(index int) (value int, found bool)
- func (a *SortedIntArray) RemoveValue(value int) bool
- func (a *SortedIntArray) RemoveValues(values ...int)
- func (a *SortedIntArray) Search(value int) (index int)
- func (a *SortedIntArray) SetArray(array []int) *SortedIntArray
- func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray
- func (a *SortedIntArray) Slice() []int
- func (a *SortedIntArray) Sort() *SortedIntArray
- func (a *SortedIntArray) String() string
- func (a *SortedIntArray) SubSlice(offset int, length ...int) []int
- func (a *SortedIntArray) Sum() (sum int)
- func (a *SortedIntArray) Unique() *SortedIntArray
- func (a *SortedIntArray) UnmarshalJSON(b []byte) error
- func (a *SortedIntArray) UnmarshalValue(value interface{}) (err error)
- func (a *SortedIntArray) Walk(f func(value int) int) *SortedIntArray
- type SortedStrArray
- func NewSortedStrArray(safe ...bool) *SortedStrArray
- func NewSortedStrArrayComparator(comparator func(a, b string) int, safe ...bool) *SortedStrArray
- func NewSortedStrArrayFrom(array []string, safe ...bool) *SortedStrArray
- func NewSortedStrArrayFromCopy(array []string, safe ...bool) *SortedStrArray
- func NewSortedStrArraySize(cap int, safe ...bool) *SortedStrArray
- func (a *SortedStrArray) Add(values ...string) *SortedStrArray
- func (a *SortedStrArray) Append(values ...string) *SortedStrArray
- func (a *SortedStrArray) At(index int) (value string)
- func (a *SortedStrArray) Chunk(size int) [][]string
- func (a *SortedStrArray) Clear() *SortedStrArray
- func (a *SortedStrArray) Clone() (newArray *SortedStrArray)
- func (a *SortedStrArray) Contains(value string) bool
- func (a *SortedStrArray) ContainsI(value string) bool
- func (a *SortedStrArray) CountValues() map[string]int
- func (a *SortedStrArray) DeepCopy() interface{}
- func (a *SortedStrArray) Filter(filter func(index int, value string) bool) *SortedStrArray
- func (a *SortedStrArray) FilterEmpty() *SortedStrArray
- func (a *SortedStrArray) Get(index int) (value string, found bool)
- func (a *SortedStrArray) Interfaces() []interface{}
- func (a *SortedStrArray) IsEmpty() bool
- func (a *SortedStrArray) Iterator(f func(k int, v string) bool)
- func (a *SortedStrArray) IteratorAsc(f func(k int, v string) bool)
- func (a *SortedStrArray) IteratorDesc(f func(k int, v string) bool)
- func (a *SortedStrArray) Join(glue string) string
- func (a *SortedStrArray) Len() int
- func (a *SortedStrArray) LockFunc(f func(array []string)) *SortedStrArray
- func (a SortedStrArray) MarshalJSON() ([]byte, error)
- func (a *SortedStrArray) Merge(array interface{}) *SortedStrArray
- func (a *SortedStrArray) PopLeft() (value string, found bool)
- func (a *SortedStrArray) PopLefts(size int) []string
- func (a *SortedStrArray) PopRand() (value string, found bool)
- func (a *SortedStrArray) PopRands(size int) []string
- func (a *SortedStrArray) PopRight() (value string, found bool)
- func (a *SortedStrArray) PopRights(size int) []string
- func (a *SortedStrArray) RLockFunc(f func(array []string)) *SortedStrArray
- func (a *SortedStrArray) Rand() (value string, found bool)
- func (a *SortedStrArray) Rands(size int) []string
- func (a *SortedStrArray) Range(start int, end ...int) []string
- func (a *SortedStrArray) Remove(index int) (value string, found bool)
- func (a *SortedStrArray) RemoveValue(value string) bool
- func (a *SortedStrArray) RemoveValues(values ...string)
- func (a *SortedStrArray) Search(value string) (index int)
- func (a *SortedStrArray) SetArray(array []string) *SortedStrArray
- func (a *SortedStrArray) SetUnique(unique bool) *SortedStrArray
- func (a *SortedStrArray) Slice() []string
- func (a *SortedStrArray) Sort() *SortedStrArray
- func (a *SortedStrArray) String() string
- func (a *SortedStrArray) SubSlice(offset int, length ...int) []string
- func (a *SortedStrArray) Sum() (sum int)
- func (a *SortedStrArray) Unique() *SortedStrArray
- func (a *SortedStrArray) UnmarshalJSON(b []byte) error
- func (a *SortedStrArray) UnmarshalValue(value interface{}) (err error)
- func (a *SortedStrArray) Walk(f func(value string) string) *SortedStrArray
- type StrArray
- func (a *StrArray) Append(value ...string) *StrArray
- func (a *StrArray) At(index int) (value string)
- func (a *StrArray) Chunk(size int) [][]string
- func (a *StrArray) Clear() *StrArray
- func (a *StrArray) Clone() (newArray *StrArray)
- func (a *StrArray) Contains(value string) bool
- func (a *StrArray) ContainsI(value string) bool
- func (a *StrArray) CountValues() map[string]int
- func (a *StrArray) DeepCopy() interface{}
- func (a *StrArray) Fill(startIndex int, num int, value string) error
- func (a *StrArray) Filter(filter func(index int, value string) bool) *StrArray
- func (a *StrArray) FilterEmpty() *StrArray
- func (a *StrArray) Get(index int) (value string, found bool)
- func (a *StrArray) InsertAfter(index int, values ...string) error
- func (a *StrArray) InsertBefore(index int, values ...string) error
- func (a *StrArray) Interfaces() []interface{}
- func (a *StrArray) IsEmpty() bool
- func (a *StrArray) Iterator(f func(k int, v string) bool)
- func (a *StrArray) IteratorAsc(f func(k int, v string) bool)
- func (a *StrArray) IteratorDesc(f func(k int, v string) bool)
- func (a *StrArray) Join(glue string) string
- func (a *StrArray) Len() int
- func (a *StrArray) LockFunc(f func(array []string)) *StrArray
- func (a StrArray) MarshalJSON() ([]byte, error)
- func (a *StrArray) Merge(array interface{}) *StrArray
- func (a *StrArray) Pad(size int, value string) *StrArray
- func (a *StrArray) PopLeft() (value string, found bool)
- func (a *StrArray) PopLefts(size int) []string
- func (a *StrArray) PopRand() (value string, found bool)
- func (a *StrArray) PopRands(size int) []string
- func (a *StrArray) PopRight() (value string, found bool)
- func (a *StrArray) PopRights(size int) []string
- func (a *StrArray) PushLeft(value ...string) *StrArray
- func (a *StrArray) PushRight(value ...string) *StrArray
- func (a *StrArray) RLockFunc(f func(array []string)) *StrArray
- func (a *StrArray) Rand() (value string, found bool)
- func (a *StrArray) Rands(size int) []string
- func (a *StrArray) Range(start int, end ...int) []string
- func (a *StrArray) Remove(index int) (value string, found bool)
- func (a *StrArray) RemoveValue(value string) bool
- func (a *StrArray) RemoveValues(values ...string)
- func (a *StrArray) Replace(array []string) *StrArray
- func (a *StrArray) Reverse() *StrArray
- func (a *StrArray) Search(value string) int
- func (a *StrArray) Set(index int, value string) error
- func (a *StrArray) SetArray(array []string) *StrArray
- func (a *StrArray) Shuffle() *StrArray
- func (a *StrArray) Slice() []string
- func (a *StrArray) Sort(reverse ...bool) *StrArray
- func (a *StrArray) SortFunc(less func(v1, v2 string) bool) *StrArray
- func (a *StrArray) String() string
- func (a *StrArray) SubSlice(offset int, length ...int) []string
- func (a *StrArray) Sum() (sum int)
- func (a *StrArray) Unique() *StrArray
- func (a *StrArray) UnmarshalJSON(b []byte) error
- func (a *StrArray) UnmarshalValue(value interface{}) error
- func (a *StrArray) Walk(f func(value string) string) *StrArray
Examples ¶
- Array.Chunk
- Array.Contains
- Array.Filter
- Array.FilterEmpty
- Array.FilterNil
- Array.Iterator
- Array.Join
- Array.Merge
- Array.PopLeft
- Array.PopLefts
- Array.PopRand
- Array.PopRight
- Array.PopRights
- Array.Rands
- Array.Reverse
- Array.Shuffle
- IntArray.Append
- IntArray.At
- IntArray.Chunk
- IntArray.Clear
- IntArray.Clone
- IntArray.Contains
- IntArray.CountValues
- IntArray.Fill
- IntArray.Filter
- IntArray.FilterEmpty
- IntArray.Get
- IntArray.InsertAfter
- IntArray.InsertBefore
- IntArray.Interfaces
- IntArray.IsEmpty
- IntArray.Iterator
- IntArray.IteratorAsc
- IntArray.IteratorDesc
- IntArray.Join
- IntArray.Len
- IntArray.LockFunc
- IntArray.MarshalJSON
- IntArray.Merge
- IntArray.Pad
- IntArray.PopLeft
- IntArray.PopLefts
- IntArray.PopRand
- IntArray.PopRands
- IntArray.PopRight
- IntArray.PopRights
- IntArray.PushLeft
- IntArray.PushRight
- IntArray.RLockFunc
- IntArray.Rand
- IntArray.Rands
- IntArray.Range
- IntArray.Remove
- IntArray.RemoveValue
- IntArray.Replace
- IntArray.Reverse
- IntArray.Search
- IntArray.Set
- IntArray.SetArray
- IntArray.Shuffle
- IntArray.Slice
- IntArray.Sort
- IntArray.SortFunc
- IntArray.String
- IntArray.SubSlice
- IntArray.Sum
- IntArray.Unique
- IntArray.UnmarshalJSON
- IntArray.UnmarshalValue
- IntArray.Walk
- New
- NewIntArray
- NewIntArrayFrom
- NewIntArrayFromCopy
- NewIntArrayRange
- NewIntArraySize
- NewSortedStrArray
- NewSortedStrArraySize
- NewStrArray
- NewStrArrayFrom
- NewStrArrayFromCopy
- NewStrArraySize
- SortedStrArray.Add
- SortedStrArray.Append
- SortedStrArray.At
- SortedStrArray.Chunk
- SortedStrArray.Clear
- SortedStrArray.Clone
- SortedStrArray.Contains
- SortedStrArray.ContainsI
- SortedStrArray.CountValues
- SortedStrArray.Filter
- SortedStrArray.FilterEmpty
- SortedStrArray.Get
- SortedStrArray.Interfaces
- SortedStrArray.IsEmpty
- SortedStrArray.Iterator
- SortedStrArray.IteratorAsc
- SortedStrArray.IteratorDesc
- SortedStrArray.Join
- SortedStrArray.Len
- SortedStrArray.LockFunc
- SortedStrArray.MarshalJSON
- SortedStrArray.Merge
- SortedStrArray.PopLeft
- SortedStrArray.PopLefts
- SortedStrArray.PopRands
- SortedStrArray.PopRight
- SortedStrArray.PopRights
- SortedStrArray.RLockFunc
- SortedStrArray.Rand
- SortedStrArray.Rands
- SortedStrArray.Range
- SortedStrArray.Remove
- SortedStrArray.RemoveValue
- SortedStrArray.Search
- SortedStrArray.SetArray
- SortedStrArray.SetUnique
- SortedStrArray.Slice
- SortedStrArray.Sort
- SortedStrArray.String
- SortedStrArray.SubSlice
- SortedStrArray.Sum
- SortedStrArray.Unique
- SortedStrArray.UnmarshalJSON
- SortedStrArray.UnmarshalValue
- SortedStrArray.Walk
- StrArray.Append
- StrArray.At
- StrArray.Chunk
- StrArray.Clear
- StrArray.Clone
- StrArray.Contains
- StrArray.ContainsI
- StrArray.CountValues
- StrArray.Fill
- StrArray.Filter
- StrArray.FilterEmpty
- StrArray.Get
- StrArray.InsertAfter
- StrArray.InsertBefore
- StrArray.Interfaces
- StrArray.IsEmpty
- StrArray.Iterator
- StrArray.IteratorAsc
- StrArray.IteratorDesc
- StrArray.Join
- StrArray.Len
- StrArray.LockFunc
- StrArray.MarshalJSON
- StrArray.Merge
- StrArray.Pad
- StrArray.PopLeft
- StrArray.PopLefts
- StrArray.PopRand
- StrArray.PopRands
- StrArray.PopRight
- StrArray.PopRights
- StrArray.PushLeft
- StrArray.PushRight
- StrArray.RLockFunc
- StrArray.Rand
- StrArray.Rands
- StrArray.Range
- StrArray.Remove
- StrArray.RemoveValue
- StrArray.Replace
- StrArray.Reverse
- StrArray.Search
- StrArray.Set
- StrArray.SetArray
- StrArray.Shuffle
- StrArray.Slice
- StrArray.Sort
- StrArray.SortFunc
- StrArray.String
- StrArray.SubSlice
- StrArray.Sum
- StrArray.Unique
- StrArray.UnmarshalJSON
- StrArray.UnmarshalValue
- StrArray.Walk
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array is a golang array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func New ¶
New creates and returns an empty array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { // A normal array. a := garray.New() // Adding items. for i := 0; i < 10; i++ { a.Append(i) } // Print the array length. fmt.Println(a.Len()) // Print the array items. fmt.Println(a.Slice()) // Retrieve item by index. fmt.Println(a.Get(6)) // Check item existence. fmt.Println(a.Contains(6)) fmt.Println(a.Contains(100)) // Insert item before specified index. a.InsertAfter(9, 11) // Insert item after specified index. a.InsertBefore(10, 10) fmt.Println(a.Slice()) // Modify item by index. a.Set(0, 100) fmt.Println(a.Slice()) fmt.Println(a.At(0)) // Search item and return its index. fmt.Println(a.Search(5)) // Remove item by index. a.Remove(0) fmt.Println(a.Slice()) // Empty the array, removes all items of it. fmt.Println(a.Slice()) a.Clear() fmt.Println(a.Slice()) }
Output: 10 [0 1 2 3 4 5 6 7 8 9] 6 true true false [0 1 2 3 4 5 6 7 8 9 10 11] [100 1 2 3 4 5 6 7 8 9 10 11] 100 5 [1 2 3 4 5 6 7 8 9 10 11] [1 2 3 4 5 6 7 8 9 10 11] []
func NewArrayFrom ¶
NewArrayFrom creates and returns an array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewArrayFromCopy ¶
NewArrayFromCopy creates and returns an array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewArrayRange ¶
NewArrayRange creates and returns an array by a range from `start` to `end` with step value `step`.
func NewArraySize ¶
NewArraySize create and returns an array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewFromCopy ¶
NewFromCopy is alias of NewArrayFromCopy. See NewArrayFromCopy.
func (*Array) At ¶
At returns the value by the specified index. If the given `index` is out of range of the array, it returns `nil`.
func (*Array) Chunk ¶
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Chunk splits an array into multiple arrays, // the size of each array is determined by `size`. // The last chunk may contain less than size elements. fmt.Println(array.Chunk(2)) }
Output: [[1 2] [3 4] [5 6] [7 8] [9]]
func (*Array) Contains ¶
Contains checks whether a value exists in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { var array garray.StrArray array.Append("a") fmt.Println(array.Contains("a")) fmt.Println(array.Contains("A")) fmt.Println(array.ContainsI("A")) }
Output: true false true
func (*Array) CountValues ¶
CountValues counts the number of occurrences of all values in the array.
func (*Array) DeepCopy ¶
func (a *Array) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*Array) Fill ¶
Fill fills an array with num entries of the value `value`, keys starting at the `startIndex` parameter.
func (*Array) Filter ¶
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/internal/empty" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) fmt.Printf("%#v\n", array1.Filter(func(index int, value interface{}) bool { return empty.IsNil(value) }).Slice()) fmt.Printf("%#v\n", array2.Filter(func(index int, value interface{}) bool { return empty.IsEmpty(value) }).Slice()) }
Output: []interface {}{0, 1, 2, "", []interface {}{}, "john"} []interface {}{1, 2, "john"}
func (*Array) FilterEmpty ¶
FilterEmpty removes all empty value of the array. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) fmt.Printf("%#v\n", array1.FilterNil().Slice()) fmt.Printf("%#v\n", array2.FilterEmpty().Slice()) }
Output: []interface {}{0, 1, 2, "", []interface {}{}, "john"} []interface {}{1, 2, "john"}
func (*Array) FilterNil ¶
FilterNil removes all nil value of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) fmt.Printf("%#v\n", array1.FilterNil().Slice()) fmt.Printf("%#v\n", array2.FilterEmpty().Slice()) }
Output: []interface {}{0, 1, 2, "", []interface {}{}, "john"} []interface {}{1, 2, "john"}
func (*Array) Get ¶
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
func (*Array) InsertAfter ¶
InsertAfter inserts the `values` to the back of `index`.
func (*Array) InsertBefore ¶
InsertBefore inserts the `values` to the front of `index`.
func (*Array) Interfaces ¶
func (a *Array) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
func (*Array) Iterator ¶
Iterator is alias of IteratorAsc.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewArrayFrom(g.Slice{"a", "b", "c"}) // Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order // with given callback function `f`. // If `f` returns true, then it continues iterating; or false to stop. array.Iterator(func(k int, v interface{}) bool { fmt.Println(k, v) return true }) // IteratorDesc iterates the array readonly in descending order with given callback function `f`. // If `f` returns true, then it continues iterating; or false to stop. array.IteratorDesc(func(k int, v interface{}) bool { fmt.Println(k, v) return true }) }
Output: 0 a 1 b 2 c 2 c 1 b 0 a
func (*Array) IteratorAsc ¶
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*Array) IteratorDesc ¶
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*Array) Join ¶
Join joins array elements with a string `glue`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{"a", "b", "c", "d"}) fmt.Println(array.Join(",")) }
Output: a,b,c,d
func (Array) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
func (*Array) Merge ¶
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array1 := garray.NewFrom(g.Slice{1, 2}) array2 := garray.NewFrom(g.Slice{3, 4}) slice1 := g.Slice{5, 6} slice2 := []int{7, 8} slice3 := []string{"9", "0"} fmt.Println(array1.Slice()) array1.Merge(array1) array1.Merge(array2) array1.Merge(slice1) array1.Merge(slice2) array1.Merge(slice3) fmt.Println(array1.Slice()) }
Output: [1 2] [1 2 1 2 3 4 5 6 7 8 9 0]
func (*Array) Pad ¶
Pad pads array to the specified length with `value`. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of `size` is less than or equal to the length of the array then no padding takes place.
func (*Array) PopLeft ¶
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Any Pop* functions pick, delete and return the item from array. fmt.Println(array.PopLeft()) fmt.Println(array.PopLefts(2)) fmt.Println(array.PopRight()) fmt.Println(array.PopRights(2)) }
Output: 1 true [2 3] 9 true [7 8]
func (*Array) PopLefts ¶
PopLefts pops and returns `size` items from the beginning of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Any Pop* functions pick, delete and return the item from array. fmt.Println(array.PopLeft()) fmt.Println(array.PopLefts(2)) fmt.Println(array.PopRight()) fmt.Println(array.PopRights(2)) }
Output: 1 true [2 3] 9 true [7 8]
func (*Array) PopRand ¶
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Randomly retrieve and return 2 items from the array. // It does not delete the items from array. fmt.Println(array.Rands(2)) // Randomly pick and return one item from the array. // It deletes the picked up item from array. fmt.Println(array.PopRand()) }
Output:
func (*Array) PopRight ¶
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Any Pop* functions pick, delete and return the item from array. fmt.Println(array.PopLeft()) fmt.Println(array.PopLefts(2)) fmt.Println(array.PopRight()) fmt.Println(array.PopRights(2)) }
Output: 1 true [2 3] 9 true [7 8]
func (*Array) PopRights ¶
PopRights pops and returns `size` items from the end of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Any Pop* functions pick, delete and return the item from array. fmt.Println(array.PopLeft()) fmt.Println(array.PopLefts(2)) fmt.Println(array.PopRight()) fmt.Println(array.PopRights(2)) }
Output: 1 true [2 3] 9 true [7 8]
func (*Array) PushRight ¶
PushRight pushes one or multiple items to the end of array. It equals to Append.
func (*Array) Rands ¶
Rands randomly returns `size` items from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Randomly retrieve and return 2 items from the array. // It does not delete the items from array. fmt.Println(array.Rands(2)) // Randomly pick and return one item from the array. // It deletes the picked up item from array. fmt.Println(array.PopRand()) }
Output:
func (*Array) Range ¶
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
func (*Array) Remove ¶
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
func (*Array) RemoveValue ¶
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
func (*Array) RemoveValues ¶
func (a *Array) RemoveValues(values ...interface{})
RemoveValues removes multiple items by `values`.
func (*Array) Replace ¶
Replace replaces the array items by given `array` from the beginning of array.
func (*Array) Reverse ¶
Reverse makes array with elements in reverse order.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Reverse makes array with elements in reverse order. fmt.Println(array.Reverse().Slice()) }
Output: [9 8 7 6 5 4 3 2 1]
func (*Array) Search ¶
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
func (*Array) Shuffle ¶
Shuffle randomly shuffles the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) // Shuffle randomly shuffles the array. fmt.Println(array.Shuffle().Slice()) }
Output:
func (*Array) Slice ¶
func (a *Array) Slice() []interface{}
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
func (*Array) String ¶
String returns current array as a string, which implements like json.Marshal does.
func (*Array) SubSlice ¶
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
func (*Array) Unique ¶
Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]
func (*Array) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*Array) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for array.
type IntArray ¶
type IntArray struct {
// contains filtered or unexported fields
}
IntArray is a golang int array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func NewIntArray ¶
NewIntArray creates and returns an empty array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewIntArray() s.Append(10) s.Append(20) s.Append(15) s.Append(30) fmt.Println(s.Slice()) }
Output: [10 20 15 30]
func NewIntArrayFrom ¶
NewIntArrayFrom creates and returns an array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30}) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [10 20 15 30] 4 4
func NewIntArrayFromCopy ¶
NewIntArrayFromCopy creates and returns an array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFromCopy(g.SliceInt{10, 20, 15, 30}) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [10 20 15 30] 4 4
func NewIntArrayRange ¶
NewIntArrayRange creates and returns an array by a range from `start` to `end` with step value `step`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewIntArrayRange(1, 5, 1) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [1 2 3 4 5] 5 8
func NewIntArraySize ¶
NewIntArraySize create and returns an array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewIntArraySize(3, 5) s.Set(0, 10) s.Set(1, 20) s.Set(2, 15) s.Set(3, 30) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [10 20 15] 3 5
func (*IntArray) Append ¶
Append is alias of PushRight,please See PushRight.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) s.Append(96, 97, 98) fmt.Println(s) }
Output: [10,20,15,30,40,50,60] [10,20,15,30,40,50,60,96,97,98]
func (*IntArray) At ¶
At returns the value by the specified index. If the given `index` is out of range of the array, it returns `0`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30}) sAt := s.At(2) fmt.Println(sAt) }
Output: 15
func (*IntArray) Chunk ¶
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.Chunk(3) fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [[10 20 15] [30 40 50] [60]]
func (*IntArray) Clear ¶
Clear deletes all items of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Clear()) fmt.Println(s) }
Output: [10,20,15,30,40,50,60] [] []
func (*IntArray) Clone ¶
Clone returns a new array, which is a copy of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.Clone() fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [10,20,15,30,40,50,60]
func (*IntArray) Contains ¶
Contains checks whether a value exists in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s.Contains(20)) fmt.Println(s.Contains(21)) }
Output: true false
func (*IntArray) CountValues ¶
CountValues counts the number of occurrences of all values in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 15, 40, 40, 40}) fmt.Println(s.CountValues()) }
Output: map[10:1 15:2 20:1 40:3]
func (*IntArray) DeepCopy ¶
func (a *IntArray) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*IntArray) Fill ¶
Fill fills an array with num entries of the value `value`, keys starting at the `startIndex` parameter.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) s.Fill(2, 3, 99) fmt.Println(s) }
Output: [10,20,15,30,40,50,60] [10,20,99,99,99,50,60]
func (*IntArray) Filter ¶
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/internal/empty" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { array1 := garray.NewIntArrayFrom(g.SliceInt{10, 40, 50, 0, 0, 0, 60}) array2 := garray.NewIntArrayFrom(g.SliceInt{10, 4, 51, 5, 45, 50, 56}) fmt.Println(array1.Filter(func(index int, value int) bool { return empty.IsEmpty(value) })) fmt.Println(array2.Filter(func(index int, value int) bool { return value%2 == 0 })) fmt.Println(array2.Filter(func(index int, value int) bool { return value%2 == 1 })) }
Output: [10,40,50,60] [51,5,45] []
func (*IntArray) FilterEmpty ¶
FilterEmpty removes all zero value of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 40, 50, 0, 0, 0, 60}) fmt.Println(s) fmt.Println(s.FilterEmpty()) }
Output: [10,40,50,0,0,0,60] [10,40,50,60]
func (*IntArray) Get ¶
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30}) sGet, sBool := s.Get(3) fmt.Println(sGet, sBool) sGet, sBool = s.Get(99) fmt.Println(sGet, sBool) }
Output: 30 true 0 false
func (*IntArray) InsertAfter ¶
InsertAfter inserts the `value` to the back of `index`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) s.InsertAfter(1, 99) fmt.Println(s.Slice()) }
Output: [10 20 99 15 30]
func (*IntArray) InsertBefore ¶
InsertBefore inserts the `values` to the front of `index`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) s.InsertBefore(1, 99) fmt.Println(s.Slice()) }
Output: [10 99 20 15 30]
func (*IntArray) Interfaces ¶
func (a *IntArray) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) r := s.Interfaces() fmt.Println(r) }
Output: [10 20 15 30 40 50 60]
func (*IntArray) IsEmpty ¶
IsEmpty checks whether the array is empty.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s.IsEmpty()) s1 := garray.NewIntArray() fmt.Println(s1.IsEmpty()) }
Output: false true
func (*IntArray) Iterator ¶
Iterator is alias of IteratorAsc.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.Iterator(func(k int, v int) bool { fmt.Println(k, v) return true }) }
Output: 0 10 1 20 2 15 3 30 4 40 5 50 6 60
func (*IntArray) IteratorAsc ¶
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.IteratorAsc(func(k int, v int) bool { fmt.Println(k, v) return true }) }
Output: 0 10 1 20 2 15 3 30 4 40 5 50 6 60
func (*IntArray) IteratorDesc ¶
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.IteratorDesc(func(k int, v int) bool { fmt.Println(k, v) return true }) }
Output: 6 60 5 50 4 40 3 30 2 15 1 20 0 10
func (*IntArray) Join ¶
Join joins array elements with a string `glue`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Join(",")) }
Output: [10,20,15,30,40,50,60] 10,20,15,30,40,50,60
func (*IntArray) Len ¶
Len returns the length of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Len()) }
Output: [10,20,15,30,40,50,60] 7
func (*IntArray) LockFunc ¶
LockFunc locks writing by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.LockFunc(func(array []int) { for i := 0; i < len(array)-1; i++ { fmt.Println(array[i]) } }) }
Output: 10 20 15 30 40 50
func (IntArray) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/internal/json" ) func main() { type Student struct { Id int Name string Scores garray.IntArray } var array garray.IntArray array.SetArray(g.SliceInt{98, 97, 96}) s := Student{ Id: 1, Name: "john", Scores: array, } b, _ := json.Marshal(s) fmt.Println(string(b)) }
Output: {"Id":1,"Name":"john","Scores":[98,97,96]}
func (*IntArray) Merge ¶
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s1 := garray.NewIntArray() s2 := garray.NewIntArray() s1.SetArray(g.SliceInt{10, 20, 15}) s2.SetArray(g.SliceInt{40, 50, 60}) fmt.Println(s1) fmt.Println(s2) s1.Merge(s2) fmt.Println(s1) }
Output: [10,20,15] [40,50,60] [10,20,15,40,50,60]
func (*IntArray) Pad ¶
Pad pads array to the specified length with `value`. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of `size` is less than or equal to the length of the array then no padding takes place.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.Pad(8, 99) fmt.Println(s) s.Pad(-10, 89) fmt.Println(s) }
Output: [10,20,15,30,40,50,60,99] [89,89,10,20,15,30,40,50,60,99]
func (*IntArray) PopLeft ¶
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.PopLeft() fmt.Println(s.Slice()) }
Output: [10,20,15,30] [20 15 30]
func (*IntArray) PopLefts ¶
PopLefts pops and returns `size` items from the beginning of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.PopLefts(2) fmt.Println(s) fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [15,30,40,50,60] [10 20]
func (*IntArray) PopRand ¶
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60, 70}) fmt.Println(s) r, _ := s.PopRand() fmt.Println(s) fmt.Println(r) // May Output: // [10,20,15,30,40,50,60,70] // [10,20,15,30,40,60,70] // 50 }
Output:
func (*IntArray) PopRands ¶
PopRands randomly pops and returns `size` items out of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.PopRands(2) fmt.Println(s) fmt.Println(r) // May Output: // [10,20,15,30,40,50,60] // [10,20,15,30,40] // [50 60] }
Output:
func (*IntArray) PopRight ¶
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.PopRight() fmt.Println(s.Slice()) }
Output: [10,20,15,30] [10 20 15]
func (*IntArray) PopRights ¶
PopRights pops and returns `size` items from the end of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.PopRights(2) fmt.Println(s) fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [10,20,15,30,40] [50 60]
func (*IntArray) PushLeft ¶
PushLeft pushes one or multiple items to the beginning of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.PushLeft(96, 97, 98, 99) fmt.Println(s.Slice()) }
Output: [10,20,15,30] [96 97 98 99 10 20 15 30]
func (*IntArray) PushRight ¶
PushRight pushes one or multiple items to the end of array. It equals to Append.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.PushRight(96, 97, 98, 99) fmt.Println(s.Slice()) }
Output: [10,20,15,30] [10 20 15 30 96 97 98 99]
func (*IntArray) RLockFunc ¶
RLockFunc locks reading by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) s.RLockFunc(func(array []int) { for i := 0; i < len(array); i++ { fmt.Println(array[i]) } }) }
Output: 10 20 15 30 40 50 60
func (*IntArray) Rand ¶
Rand randomly returns one item from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Rand()) // May Output: // [10,20,15,30,40,50,60] // 10 true }
Output:
func (*IntArray) Rands ¶
Rands randomly returns `size` items from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Rands(3)) // May Output: // [10,20,15,30,40,50,60] // [20 50 20] }
Output:
func (*IntArray) Range ¶
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.Range(2, 5) fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [15 30 40]
func (*IntArray) Remove ¶
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.Remove(1) fmt.Println(s.Slice()) }
Output: [10,20,15,30] [10 15 30]
func (*IntArray) RemoveValue ¶
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.RemoveValue(20) fmt.Println(s.Slice()) }
Output: [10,20,15,30] [10 15 30]
func (*IntArray) RemoveValues ¶
RemoveValues removes multiple items by `values`.
func (*IntArray) Replace ¶
Replace replaces the array items by given `array` from the beginning of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s.Slice()) s.Replace(g.SliceInt{12, 13}) fmt.Println(s.Slice()) }
Output: [10 20 15 30] [12 13 15 30]
func (*IntArray) Reverse ¶
Reverse makes array with elements in reverse order.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Reverse()) }
Output: [10,20,15,30,40,50,60] [60,50,40,30,15,20,10]
func (*IntArray) Search ¶
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s.Search(20)) fmt.Println(s.Search(21)) }
Output: 1 -1
func (*IntArray) Set ¶
Set sets value to specified index.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewIntArraySize(3, 5) s.Set(0, 10) s.Set(1, 20) s.Set(2, 15) s.Set(3, 30) fmt.Println(s.Slice()) }
Output: [10 20 15]
func (*IntArray) SetArray ¶
SetArray sets the underlying slice array with the given `array`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) fmt.Println(s.Slice()) }
Output: [10 20 15 30]
func (*IntArray) Shuffle ¶
Shuffle randomly shuffles the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.Shuffle()) // May Output: // [10,20,15,30,40,50,60] // [10,40,15,50,20,60,30] }
Output:
func (*IntArray) Slice ¶
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s.Slice()) }
Output: [10 20 15 30 40 50 60]
func (*IntArray) Sort ¶
Sort sorts the array in increasing order. The parameter `reverse` controls whether sort in increasing order(default) or decreasing order.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) a := s.Sort() fmt.Println(a) }
Output: [10,15,20,30]
func (*IntArray) SortFunc ¶
SortFunc sorts the array by custom function `less`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30}) fmt.Println(s) s.SortFunc(func(v1, v2 int) bool { // fmt.Println(v1,v2) return v1 > v2 }) fmt.Println(s) s.SortFunc(func(v1, v2 int) bool { return v1 < v2 }) fmt.Println(s) }
Output: [10,20,15,30] [30,20,15,10] [10,15,20,30]
func (*IntArray) String ¶
String returns current array as a string, which implements like json.Marshal does.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) fmt.Println(s.String()) }
Output: [10,20,15,30,40,50,60] [10,20,15,30,40,50,60]
func (*IntArray) SubSlice ¶
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60}) fmt.Println(s) r := s.SubSlice(3, 4) fmt.Println(r) }
Output: [10,20,15,30,40,50,60] [30 40 50 60]
func (*IntArray) Sum ¶
Sum returns the sum of values in an array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 30}) a := s.Sum() fmt.Println(a) }
Output: 75
func (*IntArray) Unique ¶
Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewIntArray() s.SetArray(g.SliceInt{10, 20, 15, 15, 20, 50, 60}) fmt.Println(s) fmt.Println(s.Unique()) }
Output: [10,20,15,15,20,50,60] [10,20,15,50,60]
func (*IntArray) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/internal/json" ) func main() { b := []byte(`{"Id":1,"Name":"john","Scores":[98,96,97]}`) type Student struct { Id int Name string Scores *garray.IntArray } s := Student{} json.Unmarshal(b, &s) fmt.Println(s) }
Output: {1 john [98,96,97]}
func (*IntArray) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" ) func main() { type Student struct { Name string Scores *garray.IntArray } var s *Student gconv.Struct(g.Map{ "name": "john", "scores": g.SliceInt{96, 98, 97}, }, &s) fmt.Println(s) }
Output: &{john [96,98,97]}
func (*IntArray) Walk ¶
Walk applies a user supplied function `f` to every item of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { var array garray.IntArray tables := g.SliceInt{10, 20} prefix := 99 array.Append(tables...) // Add prefix for given table names. array.Walk(func(value int) int { return prefix + value }) fmt.Println(array.Slice()) }
Output: [109 119]
type SortedArray ¶
type SortedArray struct {
// contains filtered or unexported fields
}
SortedArray is a golang sorted array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func NewSortedArray ¶
func NewSortedArray(comparator func(a, b interface{}) int, safe ...bool) *SortedArray
NewSortedArray creates and returns an empty sorted array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default. The parameter `comparator` used to compare values to sort in array, if it returns value < 0, means `a` < `b`; the `a` will be inserted before `b`; if it returns value = 0, means `a` = `b`; the `a` will be replaced by `b`; if it returns value > 0, means `a` > `b`; the `a` will be inserted after `b`;
func NewSortedArrayFrom ¶
func NewSortedArrayFrom(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
NewSortedArrayFrom creates and returns an sorted array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedArrayFromCopy ¶
func NewSortedArrayFromCopy(array []interface{}, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
NewSortedArrayFromCopy creates and returns an sorted array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedArrayRange ¶
func NewSortedArrayRange(start, end, step int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
NewSortedArrayRange creates and returns an array by a range from `start` to `end` with step value `step`.
func NewSortedArraySize ¶
func NewSortedArraySize(cap int, comparator func(a, b interface{}) int, safe ...bool) *SortedArray
NewSortedArraySize create and returns an sorted array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func (*SortedArray) Add ¶
func (a *SortedArray) Add(values ...interface{}) *SortedArray
Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.
func (*SortedArray) Append ¶
func (a *SortedArray) Append(values ...interface{}) *SortedArray
Append adds one or multiple values to sorted array, the array always keeps sorted.
func (*SortedArray) At ¶
func (a *SortedArray) At(index int) (value interface{})
At returns the value by the specified index. If the given `index` is out of range of the array, it returns `nil`.
func (*SortedArray) Chunk ¶
func (a *SortedArray) Chunk(size int) [][]interface{}
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
func (*SortedArray) Clear ¶
func (a *SortedArray) Clear() *SortedArray
Clear deletes all items of current array.
func (*SortedArray) Clone ¶
func (a *SortedArray) Clone() (newArray *SortedArray)
Clone returns a new array, which is a copy of current array.
func (*SortedArray) Contains ¶
func (a *SortedArray) Contains(value interface{}) bool
Contains checks whether a value exists in the array.
func (*SortedArray) CountValues ¶
func (a *SortedArray) CountValues() map[interface{}]int
CountValues counts the number of occurrences of all values in the array.
func (*SortedArray) DeepCopy ¶
func (a *SortedArray) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*SortedArray) Filter ¶
func (a *SortedArray) Filter(filter func(index int, value interface{}) bool) *SortedArray
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
func (*SortedArray) FilterEmpty ¶
func (a *SortedArray) FilterEmpty() *SortedArray
FilterEmpty removes all empty value of the array. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
func (*SortedArray) FilterNil ¶
func (a *SortedArray) FilterNil() *SortedArray
FilterNil removes all nil value of the array.
func (*SortedArray) Get ¶
func (a *SortedArray) Get(index int) (value interface{}, found bool)
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
func (*SortedArray) Interfaces ¶
func (a *SortedArray) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
func (*SortedArray) IsEmpty ¶
func (a *SortedArray) IsEmpty() bool
IsEmpty checks whether the array is empty.
func (*SortedArray) Iterator ¶
func (a *SortedArray) Iterator(f func(k int, v interface{}) bool)
Iterator is alias of IteratorAsc.
func (*SortedArray) IteratorAsc ¶
func (a *SortedArray) IteratorAsc(f func(k int, v interface{}) bool)
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*SortedArray) IteratorDesc ¶
func (a *SortedArray) IteratorDesc(f func(k int, v interface{}) bool)
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*SortedArray) Join ¶
func (a *SortedArray) Join(glue string) string
Join joins array elements with a string `glue`.
func (*SortedArray) LockFunc ¶
func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray
LockFunc locks writing by callback function `f`.
func (SortedArray) MarshalJSON ¶
func (a SortedArray) MarshalJSON() ([]byte, error)
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
func (*SortedArray) Merge ¶
func (a *SortedArray) Merge(array interface{}) *SortedArray
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
func (*SortedArray) PopLeft ¶
func (a *SortedArray) PopLeft() (value interface{}, found bool)
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
func (*SortedArray) PopLefts ¶
func (a *SortedArray) PopLefts(size int) []interface{}
PopLefts pops and returns `size` items from the beginning of array.
func (*SortedArray) PopRand ¶
func (a *SortedArray) PopRand() (value interface{}, found bool)
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
func (*SortedArray) PopRands ¶
func (a *SortedArray) PopRands(size int) []interface{}
PopRands randomly pops and returns `size` items out of array.
func (*SortedArray) PopRight ¶
func (a *SortedArray) PopRight() (value interface{}, found bool)
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
func (*SortedArray) PopRights ¶
func (a *SortedArray) PopRights(size int) []interface{}
PopRights pops and returns `size` items from the end of array.
func (*SortedArray) RLockFunc ¶
func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray
RLockFunc locks reading by callback function `f`.
func (*SortedArray) Rand ¶
func (a *SortedArray) Rand() (value interface{}, found bool)
Rand randomly returns one item from array(no deleting).
func (*SortedArray) Rands ¶
func (a *SortedArray) Rands(size int) []interface{}
Rands randomly returns `size` items from array(no deleting).
func (*SortedArray) Range ¶
func (a *SortedArray) Range(start int, end ...int) []interface{}
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
func (*SortedArray) Remove ¶
func (a *SortedArray) Remove(index int) (value interface{}, found bool)
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
func (*SortedArray) RemoveValue ¶
func (a *SortedArray) RemoveValue(value interface{}) bool
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
func (*SortedArray) RemoveValues ¶
func (a *SortedArray) RemoveValues(values ...interface{})
RemoveValues removes an item by `values`.
func (*SortedArray) Search ¶
func (a *SortedArray) Search(value interface{}) (index int)
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
func (*SortedArray) SetArray ¶
func (a *SortedArray) SetArray(array []interface{}) *SortedArray
SetArray sets the underlying slice array with the given `array`.
func (*SortedArray) SetComparator ¶
func (a *SortedArray) SetComparator(comparator func(a, b interface{}) int)
SetComparator sets/changes the comparator for sorting. It resorts the array as the comparator is changed.
func (*SortedArray) SetUnique ¶
func (a *SortedArray) SetUnique(unique bool) *SortedArray
SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also does unique check, remove all repeated items.
func (*SortedArray) Slice ¶
func (a *SortedArray) Slice() []interface{}
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
func (*SortedArray) Sort ¶
func (a *SortedArray) Sort() *SortedArray
Sort sorts the array in increasing order. The parameter `reverse` controls whether sort in increasing order(default) or decreasing order
func (*SortedArray) String ¶
func (a *SortedArray) String() string
String returns current array as a string, which implements like json.Marshal does.
func (*SortedArray) SubSlice ¶
func (a *SortedArray) SubSlice(offset int, length ...int) []interface{}
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
func (*SortedArray) Sum ¶
func (a *SortedArray) Sum() (sum int)
Sum returns the sum of values in an array.
func (*SortedArray) Unique ¶
func (a *SortedArray) Unique() *SortedArray
Unique uniques the array, clear repeated items.
func (*SortedArray) UnmarshalJSON ¶
func (a *SortedArray) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. Note that the comparator is set as string comparator in default.
func (*SortedArray) UnmarshalValue ¶
func (a *SortedArray) UnmarshalValue(value interface{}) (err error)
UnmarshalValue is an interface implement which sets any type of value for array. Note that the comparator is set as string comparator in default.
func (*SortedArray) Walk ¶
func (a *SortedArray) Walk(f func(value interface{}) interface{}) *SortedArray
Walk applies a user supplied function `f` to every item of array.
type SortedIntArray ¶
type SortedIntArray struct {
// contains filtered or unexported fields
}
SortedIntArray is a golang sorted int array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func NewSortedIntArray ¶
func NewSortedIntArray(safe ...bool) *SortedIntArray
NewSortedIntArray creates and returns an empty sorted array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedIntArrayComparator ¶
func NewSortedIntArrayComparator(comparator func(a, b int) int, safe ...bool) *SortedIntArray
NewSortedIntArrayComparator creates and returns an empty sorted array with specified comparator. The parameter `safe` is used to specify whether using array in concurrent-safety which is false in default.
func NewSortedIntArrayFrom ¶
func NewSortedIntArrayFrom(array []int, safe ...bool) *SortedIntArray
NewSortedIntArrayFrom creates and returns an sorted array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedIntArrayFromCopy ¶
func NewSortedIntArrayFromCopy(array []int, safe ...bool) *SortedIntArray
NewSortedIntArrayFromCopy creates and returns an sorted array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedIntArrayRange ¶
func NewSortedIntArrayRange(start, end, step int, safe ...bool) *SortedIntArray
NewSortedIntArrayRange creates and returns an array by a range from `start` to `end` with step value `step`.
func NewSortedIntArraySize ¶
func NewSortedIntArraySize(cap int, safe ...bool) *SortedIntArray
NewSortedIntArraySize create and returns an sorted array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func (*SortedIntArray) Add ¶
func (a *SortedIntArray) Add(values ...int) *SortedIntArray
Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.
func (*SortedIntArray) Append ¶
func (a *SortedIntArray) Append(values ...int) *SortedIntArray
Append adds one or multiple values to sorted array, the array always keeps sorted.
func (*SortedIntArray) At ¶
func (a *SortedIntArray) At(index int) (value int)
At returns the value by the specified index. If the given `index` is out of range of the array, it returns `0`.
func (*SortedIntArray) Chunk ¶
func (a *SortedIntArray) Chunk(size int) [][]int
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
func (*SortedIntArray) Clear ¶
func (a *SortedIntArray) Clear() *SortedIntArray
Clear deletes all items of current array.
func (*SortedIntArray) Clone ¶
func (a *SortedIntArray) Clone() (newArray *SortedIntArray)
Clone returns a new array, which is a copy of current array.
func (*SortedIntArray) Contains ¶
func (a *SortedIntArray) Contains(value int) bool
Contains checks whether a value exists in the array.
func (*SortedIntArray) CountValues ¶
func (a *SortedIntArray) CountValues() map[int]int
CountValues counts the number of occurrences of all values in the array.
func (*SortedIntArray) DeepCopy ¶
func (a *SortedIntArray) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*SortedIntArray) Filter ¶
func (a *SortedIntArray) Filter(filter func(index int, value int) bool) *SortedIntArray
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
func (*SortedIntArray) FilterEmpty ¶
func (a *SortedIntArray) FilterEmpty() *SortedIntArray
FilterEmpty removes all zero value of the array.
func (*SortedIntArray) Get ¶
func (a *SortedIntArray) Get(index int) (value int, found bool)
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
func (*SortedIntArray) Interfaces ¶
func (a *SortedIntArray) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
func (*SortedIntArray) IsEmpty ¶
func (a *SortedIntArray) IsEmpty() bool
IsEmpty checks whether the array is empty.
func (*SortedIntArray) Iterator ¶
func (a *SortedIntArray) Iterator(f func(k int, v int) bool)
Iterator is alias of IteratorAsc.
func (*SortedIntArray) IteratorAsc ¶
func (a *SortedIntArray) IteratorAsc(f func(k int, v int) bool)
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*SortedIntArray) IteratorDesc ¶
func (a *SortedIntArray) IteratorDesc(f func(k int, v int) bool)
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
func (*SortedIntArray) Join ¶
func (a *SortedIntArray) Join(glue string) string
Join joins array elements with a string `glue`.
func (*SortedIntArray) LockFunc ¶
func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray
LockFunc locks writing by callback function `f`.
func (SortedIntArray) MarshalJSON ¶
func (a SortedIntArray) MarshalJSON() ([]byte, error)
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
func (*SortedIntArray) Merge ¶
func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
func (*SortedIntArray) PopLeft ¶
func (a *SortedIntArray) PopLeft() (value int, found bool)
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
func (*SortedIntArray) PopLefts ¶
func (a *SortedIntArray) PopLefts(size int) []int
PopLefts pops and returns `size` items from the beginning of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
func (*SortedIntArray) PopRand ¶
func (a *SortedIntArray) PopRand() (value int, found bool)
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
func (*SortedIntArray) PopRands ¶
func (a *SortedIntArray) PopRands(size int) []int
PopRands randomly pops and returns `size` items out of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
func (*SortedIntArray) PopRight ¶
func (a *SortedIntArray) PopRight() (value int, found bool)
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
func (*SortedIntArray) PopRights ¶
func (a *SortedIntArray) PopRights(size int) []int
PopRights pops and returns `size` items from the end of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
func (*SortedIntArray) RLockFunc ¶
func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray
RLockFunc locks reading by callback function `f`.
func (*SortedIntArray) Rand ¶
func (a *SortedIntArray) Rand() (value int, found bool)
Rand randomly returns one item from array(no deleting).
func (*SortedIntArray) Rands ¶
func (a *SortedIntArray) Rands(size int) []int
Rands randomly returns `size` items from array(no deleting).
func (*SortedIntArray) Range ¶
func (a *SortedIntArray) Range(start int, end ...int) []int
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
func (*SortedIntArray) Remove ¶
func (a *SortedIntArray) Remove(index int) (value int, found bool)
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
func (*SortedIntArray) RemoveValue ¶
func (a *SortedIntArray) RemoveValue(value int) bool
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
func (*SortedIntArray) RemoveValues ¶
func (a *SortedIntArray) RemoveValues(values ...int)
RemoveValues removes an item by `values`.
func (*SortedIntArray) Search ¶
func (a *SortedIntArray) Search(value int) (index int)
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
func (*SortedIntArray) SetArray ¶
func (a *SortedIntArray) SetArray(array []int) *SortedIntArray
SetArray sets the underlying slice array with the given `array`.
func (*SortedIntArray) SetUnique ¶
func (a *SortedIntArray) SetUnique(unique bool) *SortedIntArray
SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also do unique check, remove all repeated items.
func (*SortedIntArray) Slice ¶
func (a *SortedIntArray) Slice() []int
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
func (*SortedIntArray) Sort ¶
func (a *SortedIntArray) Sort() *SortedIntArray
Sort sorts the array in increasing order. The parameter `reverse` controls whether sort in increasing order(default) or decreasing order.
func (*SortedIntArray) String ¶
func (a *SortedIntArray) String() string
String returns current array as a string, which implements like json.Marshal does.
func (*SortedIntArray) SubSlice ¶
func (a *SortedIntArray) SubSlice(offset int, length ...int) []int
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
func (*SortedIntArray) Sum ¶
func (a *SortedIntArray) Sum() (sum int)
Sum returns the sum of values in an array.
func (*SortedIntArray) Unique ¶
func (a *SortedIntArray) Unique() *SortedIntArray
Unique uniques the array, clear repeated items.
func (*SortedIntArray) UnmarshalJSON ¶
func (a *SortedIntArray) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*SortedIntArray) UnmarshalValue ¶
func (a *SortedIntArray) UnmarshalValue(value interface{}) (err error)
UnmarshalValue is an interface implement which sets any type of value for array.
func (*SortedIntArray) Walk ¶
func (a *SortedIntArray) Walk(f func(value int) int) *SortedIntArray
Walk applies a user supplied function `f` to every item of array.
type SortedStrArray ¶
type SortedStrArray struct {
// contains filtered or unexported fields
}
SortedStrArray is a golang sorted string array with rich features. It is using increasing order in default, which can be changed by setting it a custom comparator. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func NewSortedStrArray ¶
func NewSortedStrArray(safe ...bool) *SortedStrArray
NewSortedStrArray creates and returns an empty sorted array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArray() s.Append("b") s.Append("d") s.Append("c") s.Append("a") fmt.Println(s.Slice()) }
Output: [a b c d]
func NewSortedStrArrayComparator ¶
func NewSortedStrArrayComparator(comparator func(a, b string) int, safe ...bool) *SortedStrArray
NewSortedStrArrayComparator creates and returns an empty sorted array with specified comparator. The parameter `safe` is used to specify whether using array in concurrent-safety which is false in default.
func NewSortedStrArrayFrom ¶
func NewSortedStrArrayFrom(array []string, safe ...bool) *SortedStrArray
NewSortedStrArrayFrom creates and returns an sorted array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedStrArrayFromCopy ¶
func NewSortedStrArrayFromCopy(array []string, safe ...bool) *SortedStrArray
NewSortedStrArrayFromCopy creates and returns an sorted array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
func NewSortedStrArraySize ¶
func NewSortedStrArraySize(cap int, safe ...bool) *SortedStrArray
NewSortedStrArraySize create and returns an sorted array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArraySize(3) s.SetArray([]string{"b", "d", "a", "c"}) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [a b c d] 4 4
func (*SortedStrArray) Add ¶
func (a *SortedStrArray) Add(values ...string) *SortedStrArray
Add adds one or multiple values to sorted array, the array always keeps sorted. It's alias of function Append, see Append.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArray() s.Add("b", "d", "c", "a") fmt.Println(s) }
Output: ["a","b","c","d"]
func (*SortedStrArray) Append ¶
func (a *SortedStrArray) Append(values ...string) *SortedStrArray
Append adds one or multiple values to sorted array, the array always keeps sorted.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "c", "a"}) fmt.Println(s) s.Append("f", "e", "g") fmt.Println(s) }
Output: ["a","b","c","d"] ["a","b","c","d","e","f","g"]
func (*SortedStrArray) At ¶
func (a *SortedStrArray) At(index int) (value string)
At returns the value by the specified index. If the given `index` is out of range of the array, it returns an empty string.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "d", "c", "a"}) sAt := s.At(2) fmt.Println(s) fmt.Println(sAt) }
Output: ["a","b","c","d"] c
func (*SortedStrArray) Chunk ¶
func (a *SortedStrArray) Chunk(size int) [][]string
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.Chunk(3) fmt.Println(r) }
Output: [[a b c] [d e f] [g h]]
func (*SortedStrArray) Clear ¶
func (a *SortedStrArray) Clear() *SortedStrArray
Clear deletes all items of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s) fmt.Println(s.Clear()) fmt.Println(s) }
Output: ["a","b","c","d","e","f","g","h"] [] []
func (*SortedStrArray) Clone ¶
func (a *SortedStrArray) Clone() (newArray *SortedStrArray)
Clone returns a new array, which is a copy of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.Clone() fmt.Println(r) fmt.Println(s) }
Output: ["a","b","c","d","e","f","g","h"] ["a","b","c","d","e","f","g","h"]
func (*SortedStrArray) Contains ¶
func (a *SortedStrArray) Contains(value string) bool
Contains checks whether a value exists in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s.Contains("e")) fmt.Println(s.Contains("E")) fmt.Println(s.Contains("z")) }
Output: true false false
func (*SortedStrArray) ContainsI ¶
func (a *SortedStrArray) ContainsI(value string) bool
ContainsI checks whether a value exists in the array with case-insensitively. Note that it internally iterates the whole array to do the comparison with case-insensitively.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s) fmt.Println(s.ContainsI("E")) fmt.Println(s.ContainsI("z")) }
Output: ["a","b","c","d","e","f","g","h"] true false
func (*SortedStrArray) CountValues ¶
func (a *SortedStrArray) CountValues() map[string]int
CountValues counts the number of occurrences of all values in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"}) fmt.Println(s.CountValues()) }
Output: map[a:1 b:1 c:3 d:2]
func (*SortedStrArray) DeepCopy ¶
func (a *SortedStrArray) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*SortedStrArray) Filter ¶
func (a *SortedStrArray) Filter(filter func(index int, value string) bool) *SortedStrArray
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/internal/empty" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"}) fmt.Println(s) fmt.Println(s.Filter(func(index int, value string) bool { return empty.IsEmpty(value) })) }
Output: ["","","","a","b","c","d"] ["a","b","c","d"]
func (*SortedStrArray) FilterEmpty ¶
func (a *SortedStrArray) FilterEmpty() *SortedStrArray
FilterEmpty removes all empty string value of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"}) fmt.Println(s) fmt.Println(s.FilterEmpty()) }
Output: ["","","","a","b","c","d"] ["a","b","c","d"]
func (*SortedStrArray) Get ¶
func (a *SortedStrArray) Get(index int) (value string, found bool)
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "d", "c", "a", "e"}) sGet, sBool := s.Get(3) fmt.Println(s) fmt.Println(sGet, sBool) }
Output: ["a","b","c","d","e"] d true
func (*SortedStrArray) Interfaces ¶
func (a *SortedStrArray) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.Interfaces() fmt.Println(r) }
Output: [a b c d e f g h]
func (*SortedStrArray) IsEmpty ¶
func (a *SortedStrArray) IsEmpty() bool
IsEmpty checks whether the array is empty.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"}) fmt.Println(s.IsEmpty()) s1 := garray.NewSortedStrArray() fmt.Println(s1.IsEmpty()) }
Output: false true
func (*SortedStrArray) Iterator ¶
func (a *SortedStrArray) Iterator(f func(k int, v string) bool)
Iterator is alias of IteratorAsc.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s.Iterator(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 0 a 1 b 2 c
func (*SortedStrArray) IteratorAsc ¶
func (a *SortedStrArray) IteratorAsc(f func(k int, v string) bool)
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s.IteratorAsc(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 0 a 1 b 2 c
func (*SortedStrArray) IteratorDesc ¶
func (a *SortedStrArray) IteratorDesc(f func(k int, v string) bool)
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s.IteratorDesc(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 2 c 1 b 0 a
func (*SortedStrArray) Join ¶
func (a *SortedStrArray) Join(glue string) string
Join joins array elements with a string `glue`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s.Join(",")) }
Output: a,b,c,d,e,f,g,h
func (*SortedStrArray) Len ¶
func (a *SortedStrArray) Len() int
Len returns the length of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s) fmt.Println(s.Len()) }
Output: ["a","b","c","d","e","f","g","h"] 8
func (*SortedStrArray) LockFunc ¶
func (a *SortedStrArray) LockFunc(f func(array []string)) *SortedStrArray
LockFunc locks writing by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s.LockFunc(func(array []string) { array[len(array)-1] = "GF fans" }) fmt.Println(s) }
Output: ["a","b","GF fans"]
func (SortedStrArray) MarshalJSON ¶
func (a SortedStrArray) MarshalJSON() ([]byte, error)
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/internal/json" ) func main() { type Student struct { ID int Name string Levels garray.SortedStrArray } r := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s := Student{ ID: 1, Name: "john", Levels: *r, } b, _ := json.Marshal(s) fmt.Println(string(b)) }
Output: {"ID":1,"Name":"john","Levels":["a","b","c"]}
func (*SortedStrArray) Merge ¶
func (a *SortedStrArray) Merge(array interface{}) *SortedStrArray
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s1 := garray.NewSortedStrArray() s2 := garray.NewSortedStrArray() s1.SetArray(g.SliceStr{"b", "c", "a"}) s2.SetArray(g.SliceStr{"e", "d", "f"}) fmt.Println(s1) fmt.Println(s2) s1.Merge(s2) fmt.Println(s1) }
Output: ["a","b","c"] ["d","e","f"] ["a","b","c","d","e","f"]
func (*SortedStrArray) PopLeft ¶
func (a *SortedStrArray) PopLeft() (value string, found bool)
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "c", "a"}) r, _ := s.PopLeft() fmt.Println(r) fmt.Println(s.Slice()) }
Output: a [b c d]
func (*SortedStrArray) PopLefts ¶
func (a *SortedStrArray) PopLefts(size int) []string
PopLefts pops and returns `size` items from the beginning of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.PopLefts(2) fmt.Println(r) fmt.Println(s) }
Output: [a b] ["c","d","e","f","g","h"]
func (*SortedStrArray) PopRand ¶
func (a *SortedStrArray) PopRand() (value string, found bool)
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
func (*SortedStrArray) PopRands ¶
func (a *SortedStrArray) PopRands(size int) []string
PopRands randomly pops and returns `size` items out of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.PopRands(2) fmt.Println(r) fmt.Println(s) // May Output: // [d a] // ["b","c","e","f","g","h"] }
Output:
func (*SortedStrArray) PopRight ¶
func (a *SortedStrArray) PopRight() (value string, found bool)
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "c", "a"}) fmt.Println(s.Slice()) r, _ := s.PopRight() fmt.Println(r) fmt.Println(s.Slice()) }
Output: [a b c d] d [a b c]
func (*SortedStrArray) PopRights ¶
func (a *SortedStrArray) PopRights(size int) []string
PopRights pops and returns `size` items from the end of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.PopRights(2) fmt.Println(r) fmt.Println(s) }
Output: [g h] ["a","b","c","d","e","f"]
func (*SortedStrArray) RLockFunc ¶
func (a *SortedStrArray) RLockFunc(f func(array []string)) *SortedStrArray
RLockFunc locks reading by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) s.RLockFunc(func(array []string) { array[len(array)-1] = "GF fans" fmt.Println(array[len(array)-1]) }) fmt.Println(s) }
Output: GF fans ["a","b","GF fans"]
func (*SortedStrArray) Rand ¶
func (a *SortedStrArray) Rand() (value string, found bool)
Rand randomly returns one item from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r, _ := s.PopRand() fmt.Println(r) fmt.Println(s) // May Output: // b // ["a","c","d","e","f","g","h"] }
Output:
func (*SortedStrArray) Rands ¶
func (a *SortedStrArray) Rands(size int) []string
Rands randomly returns `size` items from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s) fmt.Println(s.Rands(3)) // May Output: // ["a","b","c","d","e","f","g","h"] // [h g c] }
Output:
func (*SortedStrArray) Range ¶
func (a *SortedStrArray) Range(start int, end ...int) []string
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.Range(2, 5) fmt.Println(r) }
Output: [c d e]
func (*SortedStrArray) Remove ¶
func (a *SortedStrArray) Remove(index int) (value string, found bool)
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "c", "a"}) fmt.Println(s.Slice()) s.Remove(1) fmt.Println(s.Slice()) }
Output: [a b c d] [a c d]
func (*SortedStrArray) RemoveValue ¶
func (a *SortedStrArray) RemoveValue(value string) bool
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "c", "a"}) fmt.Println(s.Slice()) s.RemoveValue("b") fmt.Println(s.Slice()) }
Output: [a b c d] [a c d]
func (*SortedStrArray) RemoveValues ¶
func (a *SortedStrArray) RemoveValues(values ...string)
RemoveValues removes an item by `values`.
func (*SortedStrArray) Search ¶
func (a *SortedStrArray) Search(value string) (index int)
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s) fmt.Println(s.Search("e")) fmt.Println(s.Search("E")) fmt.Println(s.Search("z")) }
Output: ["a","b","c","d","e","f","g","h"] 4 -1 -1
func (*SortedStrArray) SetArray ¶
func (a *SortedStrArray) SetArray(array []string) *SortedStrArray
SetArray sets the underlying slice array with the given `array`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArray() s.SetArray([]string{"b", "d", "a", "c"}) fmt.Println(s.Slice()) }
Output: [a b c d]
func (*SortedStrArray) SetUnique ¶
func (a *SortedStrArray) SetUnique(unique bool) *SortedStrArray
SetUnique sets unique mark to the array, which means it does not contain any repeated items. It also do unique check, remove all repeated items.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArray() s.SetArray([]string{"b", "d", "a", "c", "c", "a"}) fmt.Println(s.SetUnique(true)) }
Output: ["a","b","c","d"]
func (*SortedStrArray) Slice ¶
func (a *SortedStrArray) Slice() []string
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) fmt.Println(s.Slice()) }
Output: [a b c d e f g h]
func (*SortedStrArray) Sort ¶
func (a *SortedStrArray) Sort() *SortedStrArray
Sort sorts the array in increasing order. The parameter `reverse` controls whether sort in increasing order(default) or decreasing order.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"b", "d", "a", "c"}) fmt.Println(s) a := s.Sort() fmt.Println(a) }
Output: ["a","b","c","d"] ["a","b","c","d"]
func (*SortedStrArray) String ¶
func (a *SortedStrArray) String() string
String returns current array as a string, which implements like json.Marshal does.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"}) fmt.Println(s.String()) }
Output: ["a","b","c"]
func (*SortedStrArray) SubSlice ¶
func (a *SortedStrArray) SubSlice(offset int, length ...int) []string
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"}) r := s.SubSlice(3, 4) fmt.Println(s.Slice()) fmt.Println(r) }
Output: [a b c d e f g h] [d e f g]
func (*SortedStrArray) Sum ¶
func (a *SortedStrArray) Sum() (sum int)
Sum returns the sum of values in an array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewSortedStrArray() s.SetArray([]string{"5", "3", "2"}) fmt.Println(s) a := s.Sum() fmt.Println(a) }
Output: ["2","3","5"] 10
func (*SortedStrArray) Unique ¶
func (a *SortedStrArray) Unique() *SortedStrArray
Unique uniques the array, clear repeated items.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"}) fmt.Println(s) fmt.Println(s.Unique()) }
Output: ["a","b","c","c","c","d","d"] ["a","b","c","d"]
func (*SortedStrArray) UnmarshalJSON ¶
func (a *SortedStrArray) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/internal/json" ) func main() { b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`) type Student struct { Id int Name string Lessons *garray.StrArray } s := Student{} json.Unmarshal(b, &s) fmt.Println(s) }
Output: {1 john ["Math","English","Sport"]}
func (*SortedStrArray) UnmarshalValue ¶
func (a *SortedStrArray) UnmarshalValue(value interface{}) (err error)
UnmarshalValue is an interface implement which sets any type of value for array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" ) func main() { type Student struct { Name string Lessons *garray.StrArray } var s *Student gconv.Struct(g.Map{ "name": "john", "lessons": []byte(`["Math","English","Sport"]`), }, &s) fmt.Println(s) var s1 *Student gconv.Struct(g.Map{ "name": "john", "lessons": g.SliceStr{"Math", "English", "Sport"}, }, &s1) fmt.Println(s1) }
Output: &{john ["Math","English","Sport"]} &{john ["Math","English","Sport"]}
func (*SortedStrArray) Walk ¶
func (a *SortedStrArray) Walk(f func(value string) string) *SortedStrArray
Walk applies a user supplied function `f` to every item of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { var array garray.SortedStrArray tables := g.SliceStr{"user", "user_detail"} prefix := "gf_" array.Append(tables...) // Add prefix for given table names. array.Walk(func(value string) string { return prefix + value }) fmt.Println(array.Slice()) }
Output: [gf_user gf_user_detail]
type StrArray ¶
type StrArray struct {
// contains filtered or unexported fields
}
StrArray is a golang string array with rich features. It contains a concurrent-safe/unsafe switch, which should be set when its initialization and cannot be changed then.
func NewStrArray ¶
NewStrArray creates and returns an empty array. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewStrArray() s.Append("We") s.Append("are") s.Append("GF") s.Append("fans") fmt.Println(s.Slice()) }
Output: [We are GF fans]
func NewStrArrayFrom ¶
NewStrArrayFrom creates and returns an array with given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"}) fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [We are GF fans !] 5 5
func NewStrArrayFromCopy ¶
NewStrArrayFromCopy creates and returns an array from a copy of given slice `array`. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewSortedStrArrayFromCopy(g.SliceStr{"b", "d", "c", "a"}) fmt.Println(s.Slice()) }
Output: [a b c d]
func NewStrArraySize ¶
NewStrArraySize create and returns an array with given size and cap. The parameter `safe` is used to specify whether using array in concurrent-safety, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewStrArraySize(3, 5) s.Set(0, "We") s.Set(1, "are") s.Set(2, "GF") s.Set(3, "fans") fmt.Println(s.Slice(), s.Len(), cap(s.Slice())) }
Output: [We are GF] 3 5
func (*StrArray) Append ¶
Append is alias of PushRight,please See PushRight.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"We", "are", "GF", "fans"}) s.Append("a", "b", "c") fmt.Println(s) }
Output: ["We","are","GF","fans","a","b","c"]
func (*StrArray) At ¶
At returns the value by the specified index. If the given `index` is out of range of the array, it returns an empty string.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"}) sAt := s.At(2) fmt.Println(sAt) }
Output: GF
func (*StrArray) Chunk ¶
Chunk splits an array into multiple arrays, the size of each array is determined by `size`. The last chunk may contain less than size elements.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.Chunk(3) fmt.Println(r) }
Output: [[a b c] [d e f] [g h]]
func (*StrArray) Clear ¶
Clear deletes all items of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s) fmt.Println(s.Clear()) fmt.Println(s) }
Output: ["a","b","c","d","e","f","g","h"] [] []
func (*StrArray) Clone ¶
Clone returns a new array, which is a copy of current array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.Clone() fmt.Println(r) fmt.Println(s) }
Output: ["a","b","c","d","e","f","g","h"] ["a","b","c","d","e","f","g","h"]
func (*StrArray) Contains ¶
Contains checks whether a value exists in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Contains("e")) fmt.Println(s.Contains("z")) }
Output: true false
func (*StrArray) ContainsI ¶
ContainsI checks whether a value exists in the array with case-insensitively. Note that it internally iterates the whole array to do the comparison with case-insensitively.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.ContainsI("E")) fmt.Println(s.ContainsI("z")) }
Output: true false
func (*StrArray) CountValues ¶
CountValues counts the number of occurrences of all values in the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"}) fmt.Println(s.CountValues()) }
Output: map[a:1 b:1 c:3 d:2]
func (*StrArray) DeepCopy ¶
func (a *StrArray) DeepCopy() interface{}
DeepCopy implements interface for deep copy of current type.
func (*StrArray) Fill ¶
Fill fills an array with num entries of the value `value`, keys starting at the `startIndex` parameter.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) s.Fill(2, 3, "here") fmt.Println(s) }
Output: ["a","b","here","here","here","f","g","h"]
func (*StrArray) Filter ¶
Filter iterates array and filters elements using custom callback function. It removes the element from array if callback function `filter` returns true, it or else does nothing and continues iterating.
Example ¶
package main import ( "fmt" "strings" "github.com/gogf/gf/v2/internal/empty" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"Math", "English", "Sport"}) s1 := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"}) fmt.Println(s1.Filter(func(index int, value string) bool { return empty.IsEmpty(value) })) fmt.Println(s.Filter(func(index int, value string) bool { return strings.Contains(value, "h") })) }
Output: ["a","b","c","d"] ["Sport"]
func (*StrArray) FilterEmpty ¶
FilterEmpty removes all empty string value of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"}) fmt.Println(s.FilterEmpty()) }
Output: ["a","b","c","d"]
func (*StrArray) Get ¶
Get returns the value by the specified index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"}) sGet, sBool := s.Get(3) fmt.Println(sGet, sBool) }
Output: fans true
func (*StrArray) InsertAfter ¶
InsertAfter inserts the `values` to the back of `index`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.InsertAfter(1, "here") fmt.Println(s.Slice()) }
Output: [a b here c d]
func (*StrArray) InsertBefore ¶
InsertBefore inserts the `values` to the front of `index`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.InsertBefore(1, "here") fmt.Println(s.Slice()) }
Output: [a here b c d]
func (*StrArray) Interfaces ¶
func (a *StrArray) Interfaces() []interface{}
Interfaces returns current array as []interface{}.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.Interfaces() fmt.Println(r) }
Output: [a b c d e f g h]
func (*StrArray) IsEmpty ¶
IsEmpty checks whether the array is empty.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"}) fmt.Println(s.IsEmpty()) s1 := garray.NewStrArray() fmt.Println(s1.IsEmpty()) }
Output: false true
func (*StrArray) Iterator ¶
Iterator is alias of IteratorAsc.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) s.Iterator(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 0 a 1 b 2 c
func (*StrArray) IteratorAsc ¶
IteratorAsc iterates the array readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) s.IteratorAsc(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 0 a 1 b 2 c
func (*StrArray) IteratorDesc ¶
IteratorDesc iterates the array readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) s.IteratorDesc(func(k int, v string) bool { fmt.Println(k, v) return true }) }
Output: 2 c 1 b 0 a
func (*StrArray) Join ¶
Join joins array elements with a string `glue`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) fmt.Println(s.Join(",")) }
Output: a,b,c
func (*StrArray) Len ¶
Len returns the length of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Len()) }
Output: 8
func (*StrArray) LockFunc ¶
LockFunc locks writing by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) s.LockFunc(func(array []string) { array[len(array)-1] = "GF fans" }) fmt.Println(s) }
Output: ["a","b","GF fans"]
func (StrArray) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/internal/json" ) func main() { type Student struct { Id int Name string Lessons []string } s := Student{ Id: 1, Name: "john", Lessons: []string{"Math", "English", "Music"}, } b, _ := json.Marshal(s) fmt.Println(string(b)) }
Output: {"Id":1,"Name":"john","Lessons":["Math","English","Music"]}
func (*StrArray) Merge ¶
Merge merges `array` into current array. The parameter `array` can be any garray or slice type. The difference between Merge and Append is Append supports only specified slice type, but Merge supports more parameter types.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s1 := garray.NewStrArray() s2 := garray.NewStrArray() s1.SetArray(g.SliceStr{"a", "b", "c"}) s2.SetArray(g.SliceStr{"d", "e", "f"}) s1.Merge(s2) fmt.Println(s1) }
Output: ["a","b","c","d","e","f"]
func (*StrArray) Pad ¶
Pad pads array to the specified length with `value`. If size is positive then the array is padded on the right, or negative on the left. If the absolute value of `size` is less than or equal to the length of the array then no padding takes place.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) s.Pad(7, "here") fmt.Println(s) s.Pad(-10, "there") fmt.Println(s) }
Output: ["a","b","c","here","here","here","here"] ["there","there","there","a","b","c","here","here","here","here"]
func (*StrArray) PopLeft ¶
PopLeft pops and returns an item from the beginning of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.PopLeft() fmt.Println(s.Slice()) }
Output: [b c d]
func (*StrArray) PopLefts ¶
PopLefts pops and returns `size` items from the beginning of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.PopLefts(2) fmt.Println(r) fmt.Println(s) }
Output: [a b] ["c","d","e","f","g","h"]
func (*StrArray) PopRand ¶
PopRand randomly pops and return an item out of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r, _ := s.PopRand() fmt.Println(r) // May Output: // e }
Output:
func (*StrArray) PopRands ¶
PopRands randomly pops and returns `size` items out of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.PopRands(2) fmt.Println(r) // May Output: // [e c] }
Output:
func (*StrArray) PopRight ¶
PopRight pops and returns an item from the end of array. Note that if the array is empty, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.PopRight() fmt.Println(s.Slice()) }
Output: [a b c]
func (*StrArray) PopRights ¶
PopRights pops and returns `size` items from the end of array. If the given `size` is greater than size of the array, it returns all elements of the array. Note that if given `size` <= 0 or the array is empty, it returns nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.PopRights(2) fmt.Println(r) fmt.Println(s) }
Output: [g h] ["a","b","c","d","e","f"]
func (*StrArray) PushLeft ¶
PushLeft pushes one or multiple items to the beginning of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.PushLeft("We", "are", "GF", "fans") fmt.Println(s.Slice()) }
Output: [We are GF fans a b c d]
func (*StrArray) PushRight ¶
PushRight pushes one or multiple items to the end of array. It equals to Append.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.PushRight("We", "are", "GF", "fans") fmt.Println(s.Slice()) }
Output: [a b c d We are GF fans]
func (*StrArray) RLockFunc ¶
RLockFunc locks reading by callback function `f`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e"}) s.RLockFunc(func(array []string) { for i := 0; i < len(array); i++ { fmt.Println(array[i]) } }) }
Output: a b c d e
func (*StrArray) Rand ¶
Rand randomly returns one item from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Rand()) // May Output: // c true }
Output:
func (*StrArray) Rands ¶
Rands randomly returns `size` items from array(no deleting).
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Rands(3)) // May Output: // [e h e] }
Output:
func (*StrArray) Range ¶
Range picks and returns items by range, like array[start:end]. Notice, if in concurrent-safe usage, it returns a copy of slice; else a pointer to the underlying data.
If `end` is negative, then the offset will start from the end of array. If `end` is omitted, then the sequence will have everything from start up until the end of the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.Range(2, 5) fmt.Println(r) }
Output: [c d e]
func (*StrArray) Remove ¶
Remove removes an item by index. If the given `index` is out of range of the array, the `found` is false.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.Remove(1) fmt.Println(s.Slice()) }
Output: [a c d]
func (*StrArray) RemoveValue ¶
RemoveValue removes an item by value. It returns true if value is found in the array, or else false if not found.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d"}) s.RemoveValue("b") fmt.Println(s.Slice()) }
Output: [a c d]
func (*StrArray) RemoveValues ¶
RemoveValues removes multiple items by `values`.
func (*StrArray) Replace ¶
Replace replaces the array items by given `array` from the beginning of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"}) fmt.Println(s.Slice()) s.Replace(g.SliceStr{"Happy", "coding"}) fmt.Println(s.Slice()) }
Output: [We are GF fans !] [Happy coding GF fans !]
func (*StrArray) Reverse ¶
Reverse makes array with elements in reverse order.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Reverse()) }
Output: ["h","g","f","e","d","c","b","a"]
func (*StrArray) Search ¶
Search searches array by `value`, returns the index of `value`, or returns -1 if not exists.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Search("e")) fmt.Println(s.Search("z")) }
Output: 4 -1
func (*StrArray) Set ¶
Set sets value to specified index.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" ) func main() { s := garray.NewStrArraySize(3, 5) s.Set(0, "We") s.Set(1, "are") s.Set(2, "GF") s.Set(3, "fans") fmt.Println(s.Slice()) }
Output: [We are GF]
func (*StrArray) SetArray ¶
SetArray sets the underlying slice array with the given `array`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"}) fmt.Println(s.Slice()) }
Output: [We are GF fans !]
func (*StrArray) Shuffle ¶
Shuffle randomly shuffles the array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Shuffle()) // May Output: // ["a","c","e","d","b","g","f","h"] }
Output:
func (*StrArray) Slice ¶
Slice returns the underlying data of array. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) fmt.Println(s.Slice()) }
Output: [a b c d e f g h]
func (*StrArray) Sort ¶
Sort sorts the array in increasing order. The parameter `reverse` controls whether sort in increasing order(default) or decreasing order
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"b", "d", "a", "c"}) a := s.Sort() fmt.Println(a) }
Output: ["a","b","c","d"]
func (*StrArray) SortFunc ¶
SortFunc sorts the array by custom function `less`.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gstr" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"b", "c", "a"}) fmt.Println(s) s.SortFunc(func(v1, v2 string) bool { return gstr.Compare(v1, v2) > 0 }) fmt.Println(s) s.SortFunc(func(v1, v2 string) bool { return gstr.Compare(v1, v2) < 0 }) fmt.Println(s) }
Output: ["b","c","a"] ["c","b","a"] ["a","b","c"]
func (*StrArray) String ¶
String returns current array as a string, which implements like json.Marshal does.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) fmt.Println(s.String()) }
Output: ["a","b","c"]
func (*StrArray) SubSlice ¶
SubSlice returns a slice of elements from the array as specified by the `offset` and `size` parameters. If in concurrent safe usage, it returns a copy of the slice; else a pointer.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Any possibility crossing the left border of array, it will fail.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"}) r := s.SubSlice(3, 4) fmt.Println(r) }
Output: [d e f g]
func (*StrArray) Sum ¶
Sum returns the sum of values in an array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"3", "5", "10"}) a := s.Sum() fmt.Println(a) }
Output: 18
func (*StrArray) Unique ¶
Unique uniques the array, clear repeated items. Example: [1,1,2,3,2] -> [1,2,3]
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { s := garray.NewStrArray() s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"}) fmt.Println(s.Unique()) }
Output: ["a","b","c","d"]
func (*StrArray) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/internal/json" ) func main() { b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`) type Student struct { Id int Name string Lessons *garray.StrArray } s := Student{} json.Unmarshal(b, &s) fmt.Println(s) }
Output: {1 john ["Math","English","Sport"]}
func (*StrArray) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" ) func main() { type Student struct { Name string Lessons *garray.StrArray } var s *Student gconv.Struct(g.Map{ "name": "john", "lessons": []byte(`["Math","English","Sport"]`), }, &s) fmt.Println(s) var s1 *Student gconv.Struct(g.Map{ "name": "john", "lessons": g.SliceStr{"Math", "English", "Sport"}, }, &s1) fmt.Println(s1) }
Output: &{john ["Math","English","Sport"]} &{john ["Math","English","Sport"]}
func (*StrArray) Walk ¶
Walk applies a user supplied function `f` to every item of array.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/container/garray" "github.com/gogf/gf/v2/frame/g" ) func main() { var array garray.StrArray tables := g.SliceStr{"user", "user_detail"} prefix := "gf_" array.Append(tables...) // Add prefix for given table names. array.Walk(func(value string) string { return prefix + value }) fmt.Println(array.Slice()) }
Output: [gf_user gf_user_detail]