This example shows how to reverse an array in golang. You can use this in your stringutils.go file for your projects
In this example we use an array of strings. the array type can be of anything but same type
const myarray = []string{"hi", "golang", "this", "is", "array"}
rev := make([]string, len(myarray))
int i will +1 every loop
int j will -1 every loop
we take object from: myarray index i
and place at: rev array index j
we take object from: myarray index j
and place at: rev array index i
so, [1,2,3] is placed [3,2,1]
thus filling rev array from start and end. meeting at the center index.
so when i == j we are at center, we need to run until i < j. because i is 0.
run until i <= j, so they meet at the center object completing both sides
for i, j := 0, len(myarray)-1; i <= j; i,j = i+1, j-1 {
rev[i], rev[j] = myarray[j], myarray[i]
}
fmt.Printf("Original Array: %+v\n", myarray)
fmt.Printf("Reversed Array: %+v\n", rev)