- Ordinary Array
- Sorted Array
Iterate*
Array TraversalPop*
Array Item PopRand/PopRand
Random Access/PopContains/ContainsI
Containment CheckFilterEmpty/FilterNil
Null Value FilteringReverse
Array ReversalShuffle
Random SortingWalk
Traversal ModificationJoin
Array Item JoinChunk
Array ChunkingMerge
Array MergingJSON
Serialization/Deserialization
Ordinary Array
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
)
func main () {
// Create a concurrency-safe int type array
a := garray.NewIntArray(true)
// Add data items
for i := 0; i < 10; i++ {
a.Append(i)
}
// Get the current array length
fmt.Println(a.Len())
// Get the current list of data items
fmt.Println(a.Slice())
// Get the item at a specified index
fmt.Println(a.Get(6))
// Insert a data item after a specified index
a.InsertAfter(9, 11)
// Insert a data item before a specified index
a.InsertBefore(10, 10)
fmt.Println(a.Slice())
// Modify the data item at a specified index
a.Set(0, 100)
fmt.Println(a.Slice())
// Search for a data item, return the found index position
fmt.Println(a.Search(5))
// Remove the data item at a specified index
a.Remove(0)
fmt.Println(a.Slice())
// Concurrency-safe, write-lock operation
a.LockFunc(func(array []int) {
// Change the last item to 100
array[len(array) - 1] = 100
})
// Concurrency-safe, read-lock operation
a.RLockFunc(func(array []int) {
fmt.Println(array[len(array) - 1])
})
// Clear the array
fmt.Println(a.Slice())
a.Clear()
fmt.Println(a.Slice())
}
After execution, the output is:
[0 1 2 3 4 5 6 7 8 9]
6 true
[0 1 2 3 4 5 6 7 8 9 10 11]
[100 1 2 3 4 5 6 7 8 9 10 11]
5
[1 2 3 4 5 6 7 8 9 10 11]
100
[1 2 3 4 5 6 7 8 9 10 100]
[]
Sorted Array
The methods of sorted arrays are similar to ordinary arrays, but with automatic sorting and uniqueness filtering features.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
)
func main () {
// Customize sorted array, descending order (SortedIntArray manages data in ascending order)
a := garray.NewSortedArray(func(v1, v2 interface{}) int {
if v1.(int) < v2.(int) {
return 1
}
if v1.(int) > v2.(int) {
return -1
}
return 0
})
// Add data
a.Add(2)
a.Add(3)
a.Add(1)
fmt.Println(a.Slice())
// Add duplicate data
a.Add(3)
fmt.Println(a.Slice())
// Retrieve data, return the last comparison index position, retrieval result
// Retrieval result: 0: match; <0: argument less than comparison value; >0: argument greater than comparison value
fmt.Println(a.Search(1))
// Set unique
a.SetUnique(true)
fmt.Println(a.Slice())
a.Add(1)
fmt.Println(a.Slice())
}
After execution, the output is:
[3 2 1]
[3 3 2 1]
3 0
[3 2 1]
[3 2 1]
Iterate*
Array Traversal
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
array := garray.NewStrArrayFrom(g.SliceStr{"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 string) 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 string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 a
// 1 b
// 2 c
// 2 c
// 1 b
// 0 a
}
Pop*
Array Item Pop
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 the 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]
}
Rand/PopRand
Random Access/Pop
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 the array.
fmt.Println(array.Rands(2))
// Randomly pick and return one item from the array.
// It deletes the picked item from the array.
fmt.Println(array.PopRand())
}
Contains/ContainsI
Containment Check
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
}
FilterEmpty/FilterNil
Null Value Filtering
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"}
}
Reverse
Array Reversal
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, 5, 3})
// Reverse makes array with elements reverse.
fmt.Println(array.Reverse().Slice())
// Output:
// [3 5 1]
}
Shuffle
Random Sorting
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())
}
Walk
Traversal Modification
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]
}
Join
Array Item Join
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
}
Chunk
Array Chunking
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]]
}
Merge
Array Merging
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]
}
JSON
Serialization/Deserialization
All container types under the garray
module implement the standard library json
data format serialization/deserialization interface.
Marshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/garray"
)
func main() {
type Student struct {
Id int
Name string
Scores *garray.IntArray
}
s := Student{
Id: 1,
Name: "john",
Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
}
After execution, the output is:
{"Id":1,"Name":"john","Scores":[100,99,98]}
Unmarshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/garray"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id int
Name string
Scores *garray.IntArray
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
}
After execution, the output is:
{1 john [100,99,98]}