In Array implementation there is one major structure:
- Array[T any] struct:
- Values []*T:
- Represents the actual values inside array struct.
- size int (inaccessible):
- Represents size of the
Values
array.
- Represents size of the
- functions map[string]any (inaccessible):
- Represents custom functions that you want to run on
Array
object byRun
function.
- Represents custom functions that you want to run on
- Values []*T:
- New[T any](functions map[string]any) (*Array[T], error):
- Creates and returns a
Array
object. - Adds functions inside the
Array
object that can be run fromRun
function. - Important Note: Functions that you send inside
New
function, have to have their first input as type of*Array[T]
.
- Creates and returns a
- Array[T any] struct:
- Run(function string, params... any) ([]reflect.Value, error):
- Receives function key as string and some parameters and runs the function with given parameters.
- Clear():
- Clears the array variable(
Values
) and setssize
to 0.
- Clears the array variable(
- PeekFirst() *T:
- Returns the first element of array.
- PeekLast() *T:
- Returns the last element of array.
- Peek(index int) *T:
- Returns index'th element of array.
- PopFirst() *T:
- Pops and returns the first element of array.
- PopLast() *T:
- Pops and returns the last element of array.
- Pop(index int) *T:
- Pops and returns index'th element of array.
- PushFirst(data *T) *T:
- Adds the given data to the beginning of array.
- PushLast(data *T) *T:
- Adds the given data to the end of array.
- Push(data *T, index int) *T:
- Adds the given data to index'th place of array.
- GetSize() int:
- Returns length of the array.
- ReturnAsSlice() []T:
- Returns a copy of the actual array(
Values
variable) insideArray
object.
- Returns a copy of the actual array(
- ReplaceValue(index int, data *T) bool:
- Replaces the value of the index element of the array with the given data.
- Displace(index1 int, index2 int) bool:
- Changes the value of the two given indexes to each other in array variable.
- Run(function string, params... any) ([]reflect.Value, error):
This is just a random simple code of how to use this structure:
package main
import (
"fmt"
// "reflect"
"github.com/golodash/structure/array"
)
type Human struct {
FirstName string
LastName string
IDNum string
}
func fun(a *array.Array[Human], i int) []Human {
fmt.Println("We are inside `fun` function and i value equals to: ", i, "\n")
return a.ReturnAsSlice()
}
func main() {
a, err := array.New[Human](map[string]any{
"fun": fun,
})
if err != nil {
panic(err.Error())
}
a.Push(&Human{
FirstName: "Mahmood",
LastName: "Heidari",
IDNum: "0860215489",
}, 0)
a.Push(&Human{
FirstName: "Ali",
LastName: "Ahmadi",
IDNum: "0860211245",
}, 0)
a.Push(&Human{
FirstName: "Ahmad",
LastName: "Mostofi",
IDNum: "0860651245",
}, 1)
a.PushLast(&Human{
FirstName: "Gholam",
LastName: "Kholi",
IDNum: "0860659845",
})
a.PushLast(&Human{
FirstName: "GholamAli",
LastName: "Akbari",
IDNum: "0869859845",
})
a.PushFirst(&Human{
FirstName: "Karim",
LastName: "Khavari",
IDNum: "9869859845",
})
values, err := a.Run("fun", 1)
if err == nil {
fmt.Println(values[0])
} else {
fmt.Println(err.Error())
}
fmt.Println(a.GetSize())
a.Displace(1, 2)
a.Pop(0)
fmt.Println(a.ReturnAsSlice())
a.Displace(0, 3)
fmt.Println(a.ReturnAsSlice())
a.Pop(2)
fmt.Println(a.ReturnAsSlice())
fmt.Println(a.GetSize())
}
Output:
We are inside `fun` function and i value equals to: 1
[{Karim Khavari 9869859845} {Ali Ahmadi 0860211245} {Ahmad Mostofi 0860651245} {Mahmood Heidari 0860215489} {Gholam Kholi 0860659845} {GholamAli Akbari 0869859845}]
6
[{Ahmad Mostofi 0860651245} {Ali Ahmadi 0860211245} {Mahmood Heidari 0860215489} {Gholam Kholi 0860659845} {GholamAli Akbari 0869859845}]
[{Gholam Kholi 0860659845} {Ali Ahmadi 0860211245} {Mahmood Heidari 0860215489} {Ahmad Mostofi 0860651245} {GholamAli Akbari 0869859845}]
[{Gholam Kholi 0860659845} {Ali Ahmadi 0860211245} {Ahmad Mostofi 0860651245} {GholamAli Akbari 0869859845}]
4