Skip to main content
Version: 2.8.x(Latest)
tip

The following list of common methods may lag behind new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/garray

Append

  • Description: Append data to the end of the array; you can add an arbitrary number of strings. The Append method is an alias for PushRight.
  • Format:
Append(value ...string) *StrArray
  • Example: Create an empty array, set data, and append new data to the end of the array.
func ExampleStrArray_Append() {
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"]
}

At

  • Description: Return the data at the specified index of the array.
  • Format:
At(index int) (value string)
  • Example: Create an array and find the data at index 2.
func ExampleStrArray_At() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sAt := s.At(2)
fmt.Println(sAt)

// Output:
// GF
}

Chunk

  • Description: Split the specified array into multiple arrays of a specified size Size, returning a value of [][]string. The last array may contain fewer elements than Size.
  • Format:
Chunk(size int) [][]string
  • Example: Create an array and split it into 3 arrays.
func ExampleStrArray_Chunk() {
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]]
}

Clear

  • Description: Delete all data in the current array.
  • Format:
Clear() *StrArray
  • Example: Create an empty array, set values, and then clear the data in the array.
func ExampleStrArray_Clear() {
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"]
// []
// []
}

Clone

  • Description: Clone the current array. Returns a copy of the array that is identical to the current array.
  • Format:
Clone() (newArray *StrArray)
  • Example: Create an empty array, set values, and then clone it into a new array.
func ExampleStrArray_Clone() {
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"]
}

Contains

  • Description: Determine if an array contains the given String value. The strings are case-sensitive. Returns a boolean value.
  • Format:
Contains(value string) bool
  • Example: Create an empty array, set values, and determine if it contains specific data e and z.
func ExampleStrArray_Contains() {
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
}

ContainsI

  • Description: Determine if an array contains the given String value. The strings are case-insensitive. Returns a boolean value.
  • Format:
ContainsI(value string) bool
  • Example: Create an empty array, set values, and determine if it contains specific data E and z.
func ExampleStrArray_ContainsI() {
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
}

CountValues

  • Description: Count the occurrences of each value in the array. Returns a value of map[string]int.
  • Format:
CountValues() map[string]int
  • Example: Create an array and count the number of occurrences of each string in the array.
func ExampleStrArray_CountValues() {
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]
}

Fill

  • Description: Fill the array with the specified value starting at the given start position startIndex. Returns an error if any.
  • Format:
Fill(startIndex int, num int, value string) error
  • Example: Create an array and fill 3 data entries from the array starting at position index 2 with the string here.
func ExampleStrArray_Fill() {
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"]
}

FilterEmpty

  • Description: Filter empty strings from the specified array.
  • Format:
FilterEmpty() *StrArray
  • Example: Create an array, assign a value, and filter out empty strings from the array.
func ExampleStrArray_FilterEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.FilterEmpty())

// Output:
// ["a","b","c","d"]
}

Get

  • Description: Return the value at the specified index in the array. The return value has two parameters: the value to be returned and whether the specified position data is found found; returns true if found, and false if not found.
  • Format:
Get(index int) (value string, found bool)
  • Example: Create an array, assign a value, and get the value at index 3 of the array.
func ExampleStrArray_Get() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sGet, sBool := s.Get(3)
fmt.Println(sGet, sBool)

// Output:
// fans true
}

InsertAfter

  • Description: Insert the value value after the specified index location in the array. Returns an error if any.
  • Format:
InsertAfter(index int, value string) error
  • Example: Create an array and insert the string here after the value at index 1.
func ExampleStrArray_InsertAfter() {
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]
}

InsertBefore

  • Description: Insert the value value before the specified index location in the array. Returns an error if any.
  • Format:
InsertBefore(index int, value string) error
  • Example: Create and initialize an array, and insert the string here before the value at index 1.
func ExampleStrArray_InsertBefore() {
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]
}

Interfaces

  • Description: Return the current array as []interface{}.
  • Format:
Interfaces() []interface{}
  • Example: Create and initialize an array, and print the contents of the returned []interface{}.
func ExampleStrArray_Interfaces() {
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]
}

IsEmpty

  • Description: Determine whether the current array is an empty array. If it is an empty array, return true. If it is not an empty array, return false.
  • Format:
IsEmpty() bool
  • Example: Create and initialize two arrays, and determine whether they are empty arrays.
func ExampleStrArray_IsEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.IsEmpty())
s1 := garray.NewStrArray()
fmt.Println(s1.IsEmpty())

// Output:
// false
// true
}

Iterator

  • Description: Array traversal.
  • Format:
Iterator(f func(k int, v string) bool)
  • Example: Create an array and traverse it.
func ExampleStrArray_Iterator() {
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
}

IteratorAsc

  • Description: Traverse the array in ascending order according to the given callback function f. If f returns true, continue traversing; otherwise, stop traversing.
  • Format:
IteratorAsc(f func(k int, v string) bool)
  • Example: Create an array and perform ascending traversal according to a custom function.
func ExampleStrArray_Iterator() {
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
}

IteratorDesc

  • Description: Traverse the array in descending order according to the given callback function f. If f returns true, continue traversing; otherwise, stop traversing.
  • Format:
IteratorAsc(f func(k int, v string) bool)
  • Example: Create an array and perform descending traversal according to a custom function.
func ExampleStrArray_IteratorDesc() {
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
}

Join

  • Description: Concatenate array elements using the given string delimiter glue.
  • Format:
Join(glue string) string
  • Example: Use the delimiter ',' to concatenate the strings in the array.
func ExampleStrArray_Join() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.Join(","))

// Output:
// a,b,c
}

Len

  • Description: Obtain the length of the array.
  • Format:
Len() int
  • Example: Create a new array, initialize it, and obtain its length.
func ExampleStrArray_Len() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Len())

// Output:
// 8
}

LockFunc

  • Description: Perform a write lock on the array with the callback function f.
  • Format:
LockFunc(f func(array []string)) *StrArray
  • Example: Create a new array, and modify the last data of the array while it is locked for writing.
func ExampleStrArray_LockFunc() {
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"]
}

MarshalJSON

  • Description: Implement the JSON serialization interface for json.Marshal.
  • Format:
MarshalJSON() ([]byte, error)
  • Example: Create new JSON format data and perform serialization operations, then print the result.
func ExampleStrArray_MarshalJSON() {
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"]}
}

Merge

  • Description: Merge arrays by combining the contents of the specified array into the current array. The parameter array can be any type such as garray or slice. The main difference between Merge and Append is that Append only supports slice types, while Merge supports more parameter types.
  • Format:
Merge(array interface{}) *StrArray
  • Example: Create two new arrays s1 and s2, and merge the data from s2 into s1.
func ExampleStrArray_Merge() {
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"]
}

NewStrArray

  • Description: Create a new array. safe is an optional parameter, boolean, which is a concurrent safety switch with the default value being False.
  • Format:
NewStrArray(safe ...bool) *StrArray
  • Example: Create an empty array and add data. At this time, no Safe parameter is specified, and it defaults to non-concurrent safety settings.
func ExampleNewStrArray() {
s := garray.NewStrArray()
s.Append("We")
s.Append("are")
s.Append("GF")
s.Append("Fans")
fmt.Println(s.Slice())

// Output:
// [We are GF Fans]
}

NewStrArrayFrom

  • Description: Create a new array based on the given array content. safe is an optional parameter, boolean, which is a concurrent safety switch with the default value being False.
  • Format:
NewStrArrayFrom(array []string, safe ...bool) *StrArray
  • Example: Create an empty array and add data based on the specified content. At this time, no Safe parameter is specified, and it defaults to non-concurrent safety settings.
func ExampleNewStrArrayFrom() {
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
}

NewStrArrayFromCopy

  • Description: Create a new array based on a copy of the given array content. safe is an optional parameter, boolean, which is a concurrent safety switch with the default value being False.
  • Format:
NewStrArrayFrom(array []string, safe ...bool) *StrArray
  • Example: Create an empty array and add data based on the specified content. At this time, no Safe parameter is specified, and it defaults to non-concurrent safety settings.
func ExampleNewStrArrayFromCopy() {
s := garray.NewStrArrayFromCopy(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))

// Output:
// [We are GF fans !] 5 5
}

NewStrArraySize

  • Description: Create a new array according to the given size and cap. safe is an optional parameter, boolean, which is a concurrent safety switch with the default value being False.
  • Format:
NewStrArraySize(size int, cap int, safe ...bool) *StrArray
  • Example: Create an empty array with Size set to 3 and Cap set to 5, and add data. Print the relevant content. At this time, no Safe parameter is specified, and it defaults to non-concurrent safety settings.
func ExampleNewStrArraySize() {
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
}

Pad

  • Description: Fill the array with the specified size size and value value. If the size size is positive, it is filled from the right side of the array. If the size size is negative, it is filled from the left side of the array. If the size size is exactly equal to the length of the array, no data will be filled.
  • Format:
Pad(size int, value string) *StrArray
  • Example: Create a new array and fill it from the left with the specified string here to size 7, then fill the array with the specified string there to size 10.
func ExampleStrArray_Pad() {
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"]
}

PopLeft

  • Description: Pop a string from the left side of the array. value is the popped string. The updated array data is the remaining data. When the array is empty, found is false.
  • Format:
PopLeft() (value string, found bool)
  • Example: Create a new array, pop the leftmost data, and print the remaining data.
func ExampleStrArray_PopLeft() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopLeft()
fmt.Println(s.Slice())

// Output:
// [b c d]
}

PopLefts

  • Description: Multiple string data is popped from the left side of the array. The return value is the popped string data, and the number of popped data is size. If size is greater than the size of the array, the method will return all the data in the array. If size <= 0 or empty, then it will return nil.
  • Format:
PopLefts(size int) []string
  • Example: Create a new array, pop the leftmost 2 data, and print the popped data and the remaining data of the original array.
func ExampleStrArray_PopLefts() {
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"]
}

PopRand

  • Description: Randomly pop 1 data from the array. The return value is the popped string data. If the array is empty, then found will return false.
  • Format:
PopRand() (value string, found bool)
  • Example: Create a new array and randomly pop 1 data from the array, then print the popped data.
func ExampleStrArray_PopRand() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r, _ := s.PopRand()
fmt.Println(r)

// May Output:
// e
}

PopRands

  • Description: Randomly pop size data from the array. The return value is the popped string data. If size <= 0 or empty, then it will return nil .
  • Format:
PopRands(size int) []string
  • Example: Create a new array and randomly pop 2 data from the array, then print the popped data.
func ExampleStrArray_PopRands() {
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]
}

PopRight

  • Description: Pop a string from the right side of the array. value is the popped string. The updated array data is the remaining data. When the array is empty, found is false.
  • Format:
PopRight() (value string, found bool)
  • Example: Create a new array, pop the rightmost data, and print the remaining data.
func ExampleStrArray_PopRight() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopRight()
fmt.Println(s.Slice())

// Output:
// [a b c]
}

PopRights

  • Description: Multiple string data is popped from the right side of the array. The return value is the popped string data, and the number of popped data is size. If size is greater than the size of the array, the method will return all the data in the array. If size <= 0 or empty, then it will return nil.
  • Format:
PopRights(size int) []string
  • Example: Create a new array, pop the rightmost 2 data, and print the popped data and the remaining data of the original array.
func ExampleStrArray_PopRights() {
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"]
}

PushLeft

  • Description: Push one or more strings onto the left side of the array.
  • Format:
PushLeft(value ...string) *StrArray
  • Example: Create a new array, push multiple strings onto the left side of the array, and print the updated data.
func ExampleStrArray_PushLeft() {
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]
}

PushRight

  • Description: Push one or more strings onto the right side of the array.
  • Format:
PushRight(value ...string) *StrArray
  • Example: Create a new array, push multiple strings onto the right side of the array, and print the updated data.
func ExampleStrArray_PushRight() {
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]
}

Rand

  • Description: Randomly select 1 string from the array (non-destructive).
  • Format:
Rand() (value string, found bool)
  • Example: Create a new array, randomly select one string from the array.
func ExampleStrArray_Rand() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rand())

// May Output:
// c true
}

Rands

  • Description: Randomly select size strings from the array (non-destructive).
  • Format:
Rands(size int) []string
  • Example: Create a new array, randomly select 3 strings from the array.
func ExampleStrArray_Rands() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rands(3))

// May Output:
// [e h e]
}

Range

  • Description: Retrieve data in the specified range of the array. If used in concurrent safety mode, this method returns a slice copy.
  • Format:
Range(start int, end ...int) []string
  • Example: Create a new array, and retrieve data from index 2 to 5.
func ExampleStrArray_Range() {
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]
}

Remove

  • Description: Remove data from the array at the position index. If index is out of bounds, found returns false.
  • Format:
Remove(index int) (value string, found bool)
  • Example: Create a new array, remove data at index 1.
func ExampleStrArray_Remove() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.Remove(1)
fmt.Println(s.Slice())

// Output:
// [a c d]
}

RemoveValue

  • Description: Remove the specified data value from the array. If value is found in the array, found returns true; otherwise, found returns false.
  • Format:
RemoveValue(value string) bool
  • Example: Create a new array, remove the value b from the array.
func ExampleStrArray_RemoveValue() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.RemoveValue("b")
fmt.Println(s.Slice())

// Output:
// [a c d]
}

Replace

  • Description: Replace the original string array with the specified string array, starting from the beginning of the original array.
  • Format:
Replace(array []string) *StrArray
  • Example: Create a new array and replace it with the specified string array.
func ExampleStrArray_Replace() {
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 !]
}

Reverse

  • Description: Reverse the entire array, for example: ["qaz","wsx","edc","rfv"] => ["rfv","edc","wsx","qaz"].
  • Format:
Replace(array []string) *StrArray
  • Example: Create a new array, initialize it, reverse it, and print it.
func ExampleStrArray_Reverse() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "m", "c"})
fmt.Println(s.Reverse())

// Output:
// ["c","m","a"]
}

RLockFunc

  • Description: Perform a read lock on the array with a custom callback function f.
  • Format:
RLockFunc(f func(array []string)) *StrArray
  • Example: Create a new array, traverse the array and print its elements in the callback function f.
func ExampleStrArray_RLockFunc() {
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
}
  • Description: Search for the specified string in the array and return the index in the array. If not found, return -1.
  • Format:
Search(value string) int
  • Example: Create a new array and search for the strings e and z in the array.
func ExampleStrArray_Search() {
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
}

Set

  • Description: Set the index position in the array to value value. If index < 0 or index is out of bounds, an error error is returned.
  • Format:
Set(index int, value string) error
  • Example: Create a new array with a length of 3. Set the array value, but the value is only set in order to index 2, because of the array's length constraint, the last value isn’t set successfully.
func ExampleStrArray_Set() {
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]
}

SetArray

  • Description: Assign value to the array according to the given slice array content
  • Format:
SetArray(array []string) *StrArray
  • Example: Create a new array, assign a value to it, and print it.
func ExampleStrArray_SetArray() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice())

// Output:
// [We are GF fans !]
}

Shuffle

  • Description: Randomly shuffle the contents of the array
  • Format:
Shuffle() *StrArray
  • Example: Create a new array, assign a value, shuffle it, and print it.
func ExampleStrArray_Shuffle() {
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"]
}

Slice

  • Description: Get the slice data of the array. Note that if it is in a concurrent safety mode, a copy is returned data, otherwise a pointer to the data is returned.
  • Format:
Shuffle() *StrArray
  • Example: Create a new array, assign a value, and print the slice data of the array.
func ExampleStrArray_Slice() {
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]
}

Sort

  • Description: Sort array contents in ascending order. reverse controls the sort direction, with true for ascending order and false for descending order.
  • Format:
Sort(reverse ...bool) *StrArray
  • Example: Create a new array, assign a value, and sort it in ascending order.
func ExampleStrArray_Sort() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"b", "d", "a", "c"})
a := s.Sort()
fmt.Println(a)

// Output:
// ["a","b","c","d"]
}

SortFunc

  • Description: Sort the content of the array using a custom function less.
  • Format:
SortFunc(less func(v1, v2 string) bool) *StrArray
  • Example: Create a new array, assign a value, first sort it in descending order using a custom function, then sort it in ascending order using a custom function, and print the results.
func ExampleStrArray_SortFunc() {
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"]
}

String

  • Description: Convert the current array to string.
  • Format:
String() string
  • Example: Create a new array, assign a value, convert the array to string, and print the result.
func ExampleStrArray_String() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.String())

// Output:
// ["a","b","c"]
}

Subslice

  • Description: Obtain a slice of the array according to the given offset offset and length parameters length. Note that if it is used in concurrent safety mode, copy data is returned, otherwise a pointer to the data is returned. If the offset offset is a non-negative number, the slice is made from the beginning of the array; otherwise, if it is negative, the slice is made from the end of the array.
  • Format:
SubSlice(offset int, length ...int)
  • Example: Create a new array, assign a value, convert the array to string, and print the result.
func ExampleStrArray_SubSlice() {
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]
}

Sum

  • Description: Sum the integer values in the array.
  • Format:
Sum() (sum int)
  • Example: Create a new array, assign a value, and sum the integer values in the array.
func ExampleStrArray_Sum() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"3", "5", "10"})
a := s.Sum()
fmt.Println(a)

// Output:
// 18
}

Unique

  • Description: Deduplicate data in the array.
  • Format:
Unique() *StrArray
  • Example: Create a new array, assign a value, and deduplicate data in the array.
func ExampleStrArray_Unique() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
fmt.Println(s.Unique())

// Output:
// ["a","b","c","d"]
}

UnmarshalJSON

  • Description: Implement the UnmarshalJSON interface of json.Unmarshal.
  • Format:
UnmarshalJSON(b []byte) error
  • Example: Create a byte slice, assign it to the structure, perform deserialization operations, and print the corresponding content.
func ExampleStrArray_UnmarshalJSON() {
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"]}
}

UnmarshalValue

  • Description: Implement the deserialization interface for arbitrary type values.
  • Format:
UnmarshalValue(value interface{}) error
  • Example: Create a structure, perform deserialization operations on its values, and print the corresponding content.
func ExampleStrArray_UnmarshalValue() {
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"]}
}

Walk

  • Description: Traverse and modify the contents of the array with the custom function f.
  • Format:
Walk(f func(value string) string) *StrArray
  • Example: Create an array, traverse and modify the contents of the array by appending prefixes to each string, and print the relevant content.
func ExampleStrArray_Walk() {
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]
}