Array - Methods - 图1tip

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:
  1. Append(value ...string) *StrArray
  • Example: Create an empty array, set data, and append new data to the end of the array.
  1. func ExampleStrArray_Append() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"We", "are", "GF", "fans"})
  4. s.Append("a", "b", "c")
  5. fmt.Println(s)
  6. // Output:
  7. // ["We","are","GF","fans","a","b","c"]
  8. }

At

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

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:
  1. Chunk(size int) [][]string
  • Example: Create an array and split it into 3 arrays.
  1. func ExampleStrArray_Chunk() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  3. r := s.Chunk(3)
  4. fmt.Println(r)
  5. // Output:
  6. // [[a b c] [d e f] [g h]]
  7. }

Clear

  • Description: Delete all data in the current array.
  • Format:
  1. Clear() *StrArray
  • Example: Create an empty array, set values, and then clear the data in the array.
  1. func ExampleStrArray_Clear() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. fmt.Println(s)
  5. fmt.Println(s.Clear())
  6. fmt.Println(s)
  7. // Output:
  8. // ["a","b","c","d","e","f","g","h"]
  9. // []
  10. // []
  11. }

Clone

  • Description: Clone the current array. Returns a copy of the array that is identical to the current array.
  • Format:
  1. Clone() (newArray *StrArray)
  • Example: Create an empty array, set values, and then clone it into a new array.
  1. func ExampleStrArray_Clone() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.Clone()
  5. fmt.Println(r)
  6. fmt.Println(s)
  7. // Output:
  8. // ["a","b","c","d","e","f","g","h"]
  9. // ["a","b","c","d","e","f","g","h"]
  10. }

Contains

  • Description: Determine if an array contains the given String value. The strings are case-sensitive. Returns a boolean value.
  • Format:
  1. Contains(value string) bool
  • Example: Create an empty array, set values, and determine if it contains specific data e and z.
  1. func ExampleStrArray_Contains() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. fmt.Println(s.Contains("e"))
  5. fmt.Println(s.Contains("z"))
  6. // Output:
  7. // true
  8. // false
  9. }

ContainsI

  • Description: Determine if an array contains the given String value. The strings are case-insensitive. Returns a boolean value.
  • Format:
  1. ContainsI(value string) bool
  • Example: Create an empty array, set values, and determine if it contains specific data E and z.
  1. func ExampleStrArray_ContainsI() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. fmt.Println(s.ContainsI("E"))
  5. fmt.Println(s.ContainsI("z"))
  6. // Output:
  7. // true
  8. // false
  9. }

CountValues

  • Description: Count the occurrences of each value in the array. Returns a value of map[string]int.
  • Format:
  1. CountValues() map[string]int
  • Example: Create an array and count the number of occurrences of each string in the array.
  1. func ExampleStrArray_CountValues() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
  3. fmt.Println(s.CountValues())
  4. // Output:
  5. // map[a:1 b:1 c:3 d:2]
  6. }

Fill

  • Description: Fill the array with the specified value starting at the given start position startIndex. Returns an error if any.
  • Format:
  1. 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.
  1. func ExampleStrArray_Fill() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  3. s.Fill(2, 3, "here")
  4. fmt.Println(s)
  5. // Output:
  6. // ["a","b","here","here","here","f","g","h"]
  7. }

FilterEmpty

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

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:
  1. Get(index int) (value string, found bool)
  • Example: Create an array, assign a value, and get the value at index 3 of the array.
  1. func ExampleStrArray_Get() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
  3. sGet, sBool := s.Get(3)
  4. fmt.Println(sGet, sBool)
  5. // Output:
  6. // fans true
  7. }

InsertAfter

  • Description: Insert the value value after the specified index location in the array. Returns an error if any.
  • Format:
  1. InsertAfter(index int, value string) error
  • Example: Create an array and insert the string here after the value at index 1.
  1. func ExampleStrArray_InsertAfter() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.InsertAfter(1, "here")
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [a b here c d]
  8. }

InsertBefore

  • Description: Insert the value value before the specified index location in the array. Returns an error if any.
  • Format:
  1. InsertBefore(index int, value string) error
  • Example: Create and initialize an array, and insert the string here before the value at index 1.
  1. func ExampleStrArray_InsertBefore() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.InsertBefore(1, "here")
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [a here b c d]
  8. }

Interfaces

  • Description: Return the current array as []interface{}.
  • Format:
  1. Interfaces() []interface{}
  • Example: Create and initialize an array, and print the contents of the returned []interface{}.
  1. func ExampleStrArray_Interfaces() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.Interfaces()
  5. fmt.Println(r)
  6. // Output:
  7. // [a b c d e f g h]
  8. }

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:
  1. IsEmpty() bool
  • Example: Create and initialize two arrays, and determine whether they are empty arrays.
  1. func ExampleStrArray_IsEmpty() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
  3. fmt.Println(s.IsEmpty())
  4. s1 := garray.NewStrArray()
  5. fmt.Println(s1.IsEmpty())
  6. // Output:
  7. // false
  8. // true
  9. }

Iterator

  • Description: Array traversal.
  • Format:
  1. Iterator(f func(k int, v string) bool)
  • Example: Create an array and traverse it.
  1. func ExampleStrArray_Iterator() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  3. s.Iterator(func(k int, v string) bool {
  4. fmt.Println(k, v)
  5. return true
  6. })
  7. // Output:
  8. // 0 a
  9. // 1 b
  10. // 2 c
  11. }

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:
  1. IteratorAsc(f func(k int, v string) bool)
  • Example: Create an array and perform ascending traversal according to a custom function.
  1. func ExampleStrArray_Iterator() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  3. s.Iterator(func(k int, v string) bool {
  4. fmt.Println(k, v)
  5. return true
  6. })
  7. // Output:
  8. // 0 a
  9. // 1 b
  10. // 2 c
  11. }

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:
  1. IteratorAsc(f func(k int, v string) bool)
  • Example: Create an array and perform descending traversal according to a custom function.
  1. func ExampleStrArray_IteratorDesc() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  3. s.IteratorDesc(func(k int, v string) bool {
  4. fmt.Println(k, v)
  5. return true
  6. })
  7. // Output:
  8. // 2 c
  9. // 1 b
  10. // 0 a
  11. }

Join

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

Len

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

LockFunc

  • Description: Perform a write lock on the array with the callback function f.
  • Format:
  1. 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.
  1. func ExampleStrArray_LockFunc() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  3. s.LockFunc(func(array []string) {
  4. array[len(array)-1] = "GF fans"
  5. })
  6. fmt.Println(s)
  7. // Output:
  8. // ["a","b","GF fans"]
  9. }

MarshalJSON

  • Description: Implement the JSON serialization interface for json.Marshal.
  • Format:
  1. MarshalJSON() ([]byte, error)
  • Example: Create new JSON format data and perform serialization operations, then print the result.
  1. func ExampleStrArray_MarshalJSON() {
  2. type Student struct {
  3. Id int
  4. Name string
  5. Lessons []string
  6. }
  7. s := Student{
  8. Id: 1,
  9. Name: "john",
  10. Lessons: []string{"Math", "English", "Music"},
  11. }
  12. b, _ := json.Marshal(s)
  13. fmt.Println(string(b))
  14. // Output:
  15. // {"Id":1,"Name":"john","Lessons":["Math","English","Music"]}
  16. }

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:
  1. Merge(array interface{}) *StrArray
  • Example: Create two new arrays s1 and s2, and merge the data from s2 into s1.
  1. func ExampleStrArray_Merge() {
  2. s1 := garray.NewStrArray()
  3. s2 := garray.NewStrArray()
  4. s1.SetArray(g.SliceStr{"a", "b", "c"})
  5. s2.SetArray(g.SliceStr{"d", "e", "f"})
  6. s1.Merge(s2)
  7. fmt.Println(s1)
  8. // Output:
  9. // ["a","b","c","d","e","f"]
  10. }

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:
  1. 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.
  1. func ExampleNewStrArray() {
  2. s := garray.NewStrArray()
  3. s.Append("We")
  4. s.Append("are")
  5. s.Append("GF")
  6. s.Append("Fans")
  7. fmt.Println(s.Slice())
  8. // Output:
  9. // [We are GF Fans]
  10. }

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:
  1. 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.
  1. func ExampleNewStrArrayFrom() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
  3. fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
  4. // Output:
  5. // [We are GF fans !] 5 5
  6. }

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:
  1. 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.
  1. func ExampleNewStrArrayFromCopy() {
  2. s := garray.NewStrArrayFromCopy(g.SliceStr{"We", "are", "GF", "fans", "!"})
  3. fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
  4. // Output:
  5. // [We are GF fans !] 5 5
  6. }

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:
  1. 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.
  1. func ExampleNewStrArraySize() {
  2. s := garray.NewStrArraySize(3, 5)
  3. s.Set(0, "We")
  4. s.Set(1, "are")
  5. s.Set(2, "GF")
  6. s.Set(3, "fans")
  7. fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
  8. // Output:
  9. // [We are GF] 3 5
  10. }

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:
  1. 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.
  1. func ExampleStrArray_Pad() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  3. s.Pad(7, "here")
  4. fmt.Println(s)
  5. s.Pad(-10, "there")
  6. fmt.Println(s)
  7. // Output:
  8. // ["a","b","c","here","here","here","here"]
  9. // ["there","there","there","a","b","c","here","here","here","here"]
  10. }

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:
  1. PopLeft() (value string, found bool)
  • Example: Create a new array, pop the leftmost data, and print the remaining data.
  1. func ExampleStrArray_PopLeft() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.PopLeft()
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [b c d]
  8. }

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:
  1. 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.
  1. func ExampleStrArray_PopLefts() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.PopLefts(2)
  5. fmt.Println(r)
  6. fmt.Println(s)
  7. // Output:
  8. // [a b]
  9. // ["c","d","e","f","g","h"]
  10. }

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:
  1. PopRand() (value string, found bool)
  • Example: Create a new array and randomly pop 1 data from the array, then print the popped data.
  1. func ExampleStrArray_PopRand() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r, _ := s.PopRand()
  5. fmt.Println(r)
  6. // May Output:
  7. // e
  8. }

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:
  1. PopRands(size int) []string
  • Example: Create a new array and randomly pop 2 data from the array, then print the popped data.
  1. func ExampleStrArray_PopRands() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.PopRands(2)
  5. fmt.Println(r)
  6. // May Output:
  7. // [e c]
  8. }

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:
  1. PopRight() (value string, found bool)
  • Example: Create a new array, pop the rightmost data, and print the remaining data.
  1. func ExampleStrArray_PopRight() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.PopRight()
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [a b c]
  8. }

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:
  1. 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.
  1. func ExampleStrArray_PopRights() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.PopRights(2)
  5. fmt.Println(r)
  6. fmt.Println(s)
  7. // Output:
  8. // [g h]
  9. // ["a","b","c","d","e","f"]
  10. }

PushLeft

  • Description: Push one or more strings onto the left side of the array.
  • Format:
  1. PushLeft(value ...string) *StrArray
  • Example: Create a new array, push multiple strings onto the left side of the array, and print the updated data.
  1. func ExampleStrArray_PushLeft() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.PushLeft("We", "are", "GF", "fans")
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [We are GF fans a b c d]
  8. }

PushRight

  • Description: Push one or more strings onto the right side of the array.
  • Format:
  1. PushRight(value ...string) *StrArray
  • Example: Create a new array, push multiple strings onto the right side of the array, and print the updated data.
  1. func ExampleStrArray_PushRight() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.PushRight("We", "are", "GF", "fans")
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [a b c d We are GF fans]
  8. }

Rand

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

Rands

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

Range

  • Description: Retrieve data in the specified range of the array. If used in concurrent safety mode, this method returns a slice copy.
  • Format:
  1. Range(start int, end ...int) []string
  • Example: Create a new array, and retrieve data from index 2 to 5.
  1. func ExampleStrArray_Range() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.Range(2, 5)
  5. fmt.Println(r)
  6. // Output:
  7. // [c d e]
  8. }

Remove

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

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:
  1. RemoveValue(value string) bool
  • Example: Create a new array, remove the value b from the array.
  1. func ExampleStrArray_RemoveValue() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d"})
  4. s.RemoveValue("b")
  5. fmt.Println(s.Slice())
  6. // Output:
  7. // [a c d]
  8. }

Replace

  • Description: Replace the original string array with the specified string array, starting from the beginning of the original array.
  • Format:
  1. Replace(array []string) *StrArray
  • Example: Create a new array and replace it with the specified string array.
  1. func ExampleStrArray_Replace() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
  4. fmt.Println(s.Slice())
  5. s.Replace(g.SliceStr{"Happy", "coding"})
  6. fmt.Println(s.Slice())
  7. // Output:
  8. // [We are GF fans !]
  9. // [Happy coding GF fans !]
  10. }

Reverse

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

RLockFunc

  • Description: Perform a read lock on the array with a custom callback function f.
  • Format:
  1. RLockFunc(f func(array []string)) *StrArray
  • Example: Create a new array, traverse the array and print its elements in the callback function f.
  1. func ExampleStrArray_RLockFunc() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e"})
  3. s.RLockFunc(func(array []string) {
  4. for i := 0; i < len(array); i++ {
  5. fmt.Println(array[i])
  6. }
  7. })
  8. // Output:
  9. // a
  10. // b
  11. // c
  12. // d
  13. // e
  14. }

Search

  • Description: Search for the specified string in the array and return the index in the array. If not found, return -1.
  • Format:
  1. Search(value string) int
  • Example: Create a new array and search for the strings e and z in the array.
  1. func ExampleStrArray_Search() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. fmt.Println(s.Search("e"))
  5. fmt.Println(s.Search("z"))
  6. // Output:
  7. // 4
  8. // -1
  9. }

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:
  1. 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.
  1. func ExampleStrArray_Set() {
  2. s := garray.NewStrArraySize(3, 5)
  3. s.Set(0, "We")
  4. s.Set(1, "are")
  5. s.Set(2, "GF")
  6. s.Set(3, "fans")
  7. fmt.Println(s.Slice())
  8. // Output:
  9. // [We are GF]
  10. }

SetArray

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

Shuffle

  • Description: Randomly shuffle the contents of the array
  • Format:
  1. Shuffle() *StrArray
  • Example: Create a new array, assign a value, shuffle it, and print it.
  1. func ExampleStrArray_Shuffle() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  3. fmt.Println(s.Shuffle())
  4. // May Output:
  5. // ["a","c","e","d","b","g","f","h"]
  6. }

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:
  1. Shuffle() *StrArray
  • Example: Create a new array, assign a value, and print the slice data of the array.
  1. func ExampleStrArray_Slice() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. fmt.Println(s.Slice())
  5. // Output:
  6. // [a b c d e f g h]
  7. }

Sort

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

SortFunc

  • Description: Sort the content of the array using a custom function less.
  • Format:
  1. 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.
  1. func ExampleStrArray_SortFunc() {
  2. s := garray.NewStrArrayFrom(g.SliceStr{"b", "c", "a"})
  3. fmt.Println(s)
  4. s.SortFunc(func(v1, v2 string) bool {
  5. return gstr.Compare(v1, v2) > 0
  6. })
  7. fmt.Println(s)
  8. s.SortFunc(func(v1, v2 string) bool {
  9. return gstr.Compare(v1, v2) < 0
  10. })
  11. fmt.Println(s)
  12. // Output:
  13. // ["b","c","a"]
  14. // ["c","b","a"]
  15. // ["a","b","c"]
  16. }

String

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

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:
  1. SubSlice(offset int, length ...int)
  • Example: Create a new array, assign a value, convert the array to string, and print the result.
  1. func ExampleStrArray_SubSlice() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
  4. r := s.SubSlice(3, 4)
  5. fmt.Println(r)
  6. // Output:
  7. // [d e f g]
  8. }

Sum

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

Unique

  • Description: Deduplicate data in the array.
  • Format:
  1. Unique() *StrArray
  • Example: Create a new array, assign a value, and deduplicate data in the array.
  1. func ExampleStrArray_Unique() {
  2. s := garray.NewStrArray()
  3. s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
  4. fmt.Println(s.Unique())
  5. // Output:
  6. // ["a","b","c","d"]
  7. }

UnmarshalJSON

  • Description: Implement the UnmarshalJSON interface of json.Unmarshal.
  • Format:
  1. UnmarshalJSON(b []byte) error
  • Example: Create a byte slice, assign it to the structure, perform deserialization operations, and print the corresponding content.
  1. func ExampleStrArray_UnmarshalJSON() {
  2. b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`)
  3. type Student struct {
  4. Id int
  5. Name string
  6. Lessons *garray.StrArray
  7. }
  8. s := Student{}
  9. json.Unmarshal(b, &s)
  10. fmt.Println(s)
  11. // Output:
  12. // {1 john ["Math","English","Sport"]}
  13. }

UnmarshalValue

  • Description: Implement the deserialization interface for arbitrary type values.
  • Format:
  1. UnmarshalValue(value interface{}) error
  • Example: Create a structure, perform deserialization operations on its values, and print the corresponding content.
  1. func ExampleStrArray_UnmarshalValue() {
  2. type Student struct {
  3. Name string
  4. Lessons *garray.StrArray
  5. }
  6. var s *Student
  7. gconv.Struct(g.Map{
  8. "name": "john",
  9. "lessons": []byte(`["Math","English","Sport"]`),
  10. }, &s)
  11. fmt.Println(s)
  12. var s1 *Student
  13. gconv.Struct(g.Map{
  14. "name": "john",
  15. "lessons": g.SliceStr{"Math", "English", "Sport"},
  16. }, &s1)
  17. fmt.Println(s1)
  18. // Output:
  19. // &{john ["Math","English","Sport"]}
  20. // &{john ["Math","English","Sport"]}
  21. }

Walk

  • Description: Traverse and modify the contents of the array with the custom function f.
  • Format:
  1. 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.
  1. func ExampleStrArray_Walk() {
  2. var array garray.StrArray
  3. tables := g.SliceStr{"user", "user_detail"}
  4. prefix := "gf_"
  5. array.Append(tables...)
  6. // Add prefix for given table names.
  7. array.Walk(func(value string) string {
  8. return prefix + value
  9. })
  10. fmt.Println(array.Slice())
  11. // Output:
  12. // [gf_user gf_user_detail]
  13. }